Strings in Java
Strings in Java
STRING CLASS
String is a class in Java library.
It contains few predefined methods, which can be used via
creating objects of String class.
String s1 = "Hello world!";
OR
String s1 = new String(Hello World);
length() method example:
public class StringDemo {
public static void main(String args[]) {
String s1= "Dot saw I was Tod";
int len = s1.length();
System.out.println( "String Length is : " + len );
}
}
Output:
String Length is : 17
equals () versus ==
String Comparisons
compareTo(Object object)
String s1 = new String("Welcome);
String s2 = "welcome";
if (s1.compareTo(s2) > 0) {
// s1 is greater than s2
}
else if (s1.compareTo(s2) == 0) {
// s1 and s2 have the same contents
}
else
// s1 is less than s2
8
else
System.out.println ("str1 does not equal str2");
if (str1.equals (str3) )
System.out.println ("str1 equals str3");
else
System.out.println ("str1 does not equal str3");
result = str1.compareTo (str3);
if(result == 0)
System.out.println ("str1 and str3 are equal");
else if(result < 0)
System.out.println ("str1 is less than str3");
else
System.out.println ("str1 is greater than str3");
str2 = "One Two Three One"; // assign a new string to str2
idx = str2.indexOf ("One");
System.out.println ("Index of first occurrence of One: " +
idx);
idx = str2.lastIndexOf("One");
System.out.println ("Index of last occurrence of One: " +
idx);
String Concatenation
String s3 = s1.concat(s2);
String s3 = s1 + s2;
s1 + s2 + s3 + s4 + s5 same as
(((s1.concat(s2)).concat(s3)).concat(s4)).co
ncat(s5);
Examples
"Welcome".toLowerCase() returns a new string,
welcome.
"Welcome".toUpperCase() returns a new string,
WELCOME.
" Welcome ".trim() returns a new string, Welcome.
"Welcome".replace('e', 'A') returns a new string,
WAlcomA.
"Welcome".replaceFirst("e", "AB") returns a new
string, WABlcome.
"Welcome".replaceall("e", "AB") returns a new string,
WABlcomAB.
"Welcome".replace("el", "AB") returns a new string,
WABcome.
+toUpperCase(): String
+trim(): String
+replace(oldChar: char,
newChar: char): String
+replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in
newString: String): String
this string with the new substring.
+replaceAll(oldString: String, Returns a new string that replace all matching substrings in this
newString: String): String
string with the new substring.
+split(delimiter: String):
Returns an array of strings consisting of the substrings split by the
String[]
delimiter.
String Comparison
StartsWith () and endsWith ()
Example:
Football. endsWith ( ball);
Football. startsWith (wood);
String Comparisons
java.lang.String
+equals(s1: String): boolean
+equalsIgnoreCase(s1: String):
boolean
+compareToIgnoreCase(s1: String):
int
+regionMatches(toffset: int, s1: String, Returns true if the specified subregion of this string exactly
offset: int, len: int): boolean
matches the specified subregion in string s1.
+regionMatches(ignoreCase: boolean, Same as the preceding method except that you can specify
toffset: int, s1: String, offset: int,
whether the match is case-sensitive.
len: int): boolean
+startsWith(prefix: String): boolean
Returns a new string that concatenate this string with string s1.
string.