CH 7 String Handling: Public Class Public Static Void
CH 7 String Handling: Public Class Public Static Void
For example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l',
and 'o'.
Unlike other programming languages, strings in Java are not primitive types
(like int, char, etc).
String trim () Removes white space from both ends of this String
int indexOf(char ch) returns the index within the String of the first
occurrence of the specified character
int lastIndexOf(char ch) returns the index within the String of the last
occurrence of the specified character
If both the strings are equal then this method returns 0 else it returns
positive or negative value.
The result is positive if the first string is lexicographically greater than the
second string else the result would be negative.
int compareToIgnoreCase(String str)
Same as compareTo except case is ignored
String s1="COMPUTER",s2="applications",s3="asha",s4="esha",s5,s6;
// Length of string
System.out.println("Length of s1= "+s1.length());
// Character at index n
System.out.println("At index of s1 "+s1.charAt(3));
// Index of character
System.out.println("First index of 'i' in s2 "+s2.indexOf('i'));
System.out.println("Last index of 'i' in s2 "+s2.lastIndexOf('i'));
System.out.println("First index of 'ei' in s2 "+s2.indexOf('e'));
//Concatenation
s5=s3.concat(" ");
s5=s5.concat(s4);
System.out.println("Concatenation 1 :" + s5);
// Case conversion
s1=s1.toLowerCase();
System.out.println("Upper to lower case of s1 "+s1);
s2=s2.toUpperCase();
System.out.println("Lower to upper case of s2 "+s2);
// Substrings
s1=s1.substring(2,5);
System.out.println("Substring of s1 "+s1);
}
}
System.out.println(s.endsWith("string")); // true
}
}
//4. String Example for ValueOf Method
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
}
}