ISC Class 11 Java - String Functions Notes
length()
Returns total number of characters.
Example: String s='COMPUTER'; [Link](); Output: 8
charAt(index)
Returns character at given index.
Example: String s='JAVA'; [Link](2); Output: V
substring(beginIndex)
Returns substring from index to end.
Example: 'COMPUTER'.substring(3)=PUTER
substring(begin,end)
Returns begin to end-1.
Example: 'COMPUTER'.substring(2,6)=MPUT
indexOf()
First occurrence.
Example: 'BANANA'.indexOf('A')=1
lastIndexOf()
Last occurrence.
Example: 'BANANA'.lastIndexOf('A')=5
equals()
Case-sensitive comparison.
Example: 'Java'.equals('Java')=true
equalsIgnoreCase()
Ignores case.
Example: 'Java'.equalsIgnoreCase('JAVA')=true
compareTo()
Lexicographic comparison.
Example: [Link](Banana)=negative
compareToIgnoreCase()
Lexicographic ignoring case.
Example: [Link](apple)=0
concat()
Joins strings.
Example: 'Hello '.concat('World')
toUpperCase()
Uppercase.
Example: computer -> COMPUTER
toLowerCase()
Lowercase.
Example: JAVA -> java
trim()
Removes leading/trailing spaces.
Example: ' JAVA ' -> JAVA
replace()
Replace all chars/substrings.
Example: APPLE -> ABBLE
replaceFirst()
Replace first occurrence.
Example: one one->two one
replaceAll()
Replace all occurrences.
Example: one one->two two
startsWith()
Checks prefix.
Example: Computer startsWith Com=true
endsWith()
Checks suffix.
Example: Computer endsWith ter=true
contains()
Checks substring.
Example: Contains Science=true
isEmpty()
Checks empty string.
Example: ''.isEmpty()=true
split()
Splits by delimiter.
Example: Red,Blue -> Red Blue
valueOf()
Converts value to String.
Example: 125->'125'
intern()
Returns pooled string.
Example: new String('Java').intern()
toCharArray()
Converts to char array.
Example: JAVA -> J A V A