What is String in Java?
String is basically an sequence of char values in Java. String is also an object in Java.
//Declaration of char array
char[] seqChar = {‘q’,’a’,’c’,’r’,’e’,’a’,’t’,’o’,’r’,’s’};
1
2
3
//Declaration of char array
char[] seqChar = {‘q’,’a’,’c’,’r’,’e’,’a’,’t’,’o’,’r’,’s’};
1 St way : Creating the String objects using object creation process using new keyword.
//we can make all this array of characters into a single word.
Instead of separating as characters.
String str = new String(seqChar);
2nd way : We can declare the string like other data types by declaring
String str = “qacreators”
Java String provides many methods to perform operations like substring(), concat(), length(), equals(), replace(), split() etc.
1.SubString() : SubString method in java will separate a part of String from the actual String and will store the separated String.
SubString Example :
1 2 3 4 5 6 7 8 9 |
public class SubString { public static void main(String args[]) { String text = "seleniumTesting"; String str1 = text.substring(3); System.out.println(str1); String str2 = text.substring(3, 8); System.out.println(str2); } } |
OUTPUT :
Java SubString Inbuilt Function
- concat() : Concat method will append one string to another String. Second String will be concatenated at the end.
Example of Concat :
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Concatenate { public static void main(String args[]) { String str1 = "selenium"; String str2 = "training"; String res1 = "Selenium".concat("training"); System.out.println(res1); String res2 = str1.concat("com"); System.out.println(res2); String res3 = str1.concat(str2); System.out.println(res3); } } |
OUTPUT :
Java concatenate Inbuilt Function
3.length() : Length method will return total number of characters available in the String.
Example of Length:
1 2 3 4 5 6 |
public class strLength { public static void main(String args[]) { String strlength = "seleniumtesting"; System.out.println("The length of the string is :"+strlength.length()); } } |
OUTPUT :
Java length Inbuilt Function
- equals() : Equals method will compare two Strings for their content. If both the String content are equal true value is returned, else false value is returned. Return type is boolean for equals method.
Example of Equals:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class equals { public static void main(String args[]) { String text = "seleniumTesting"; if(text.equals("SeleniumTesting")) System.out.println("The Both are Equal"); else System.out.println("The Both are not Equal"); if(text.equalsIgnoreCase("SEleniumTestinG")) System.out.println("The Both are Equal"); else System.out.println("The Both are not Equal"); } } |
OUTPUT :
Java Equals Inbuilt Function
- replace() : Replace method will return a String replacing all the old characters with new characters.
Example of Replace:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class replace { public static void main(String args[]) { String text = "seleniumtesting"; String str1 = text.replace('s', 'S'); System.out.println(str1); String str2 = text.replaceAll(text, "Start"); System.out.println(str2); String text1 = "Hello Java!!Hello Java!!"; String str3 = text1.replaceFirst("Hello", "Java"); System.out.println(str3); } } |
OUTPUT :
Java Replace Inbuilt Function
6.split() : Split method will split the String based on the regular expression that is passed and will return the char Array.
Example of Split:
1 2 3 4 5 6 7 8 9 |
public class split { public static void main(String args[]) { String phoneNumber= "004-345-6789"; String[] strOutput = phoneNumber.split("-"); System.out.println(strOutput[0]); System.out.println(strOutput[1]); System.out.println(strOutput[2]); } } |
OUTPUT :