Java String is defined in java.lang.String.
Java String is a sequence of characters quoted in double quote (” “).
Java String Constructors
Sr No | Constructor and description |
---|---|
1 | String() Create a new empty string object |
2 | String(byte[] bytes) Construct a string object with byte array |
3 | String(char[] value) Construct a string object with character array |
4 | String(String original) Create a string with another string |
5 | String(StringBuffer buffer) Create a string with StringBuffer object |
6 | String(StringBuilder builder) Create a string with StringBuilder object |
Java String Methods
There are many methods avaible in string class few common and widely used methods are
Java String Method charAt()
This method takes one integer argument and return a character specified at index position.
For any string the index value starts from 0 to string length -1 position.
If passed index value not in valid range then it throws IndexOutOfBoundsException.
For example string Hello Friends contains 13 characters starting from 0 to 12 H is first position with index value 0, F is in seventh position with index value 6 and s is in 13 position with index value 12.
Using number other then 0 to 12( 0 to length()-1) will result in IndexOutOfBountException
Signature:
public char charAt(int index)
Parameters:
index -Integer index value to get character at specified index position.
Return:
Character at passed index value.
Exception Throws:
IndexOutOfBoundsException If index value in not between 0 to String length()-1
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class StringChatAtExample { public static void main(String args[]) { String test = "Java String charAt() example"; char firstCharacter = test.charAt(0); char sixthCharacter = test.charAt(5); char lastCharacter = test.charAt(test.length() - 1); System.out.println("String:" + test); System.out.println("Character at first position: " + firstCharacter); System.out.println("Character at sixth position: " + sixthCharacter); System.out.println("Character at last position: " + lastCharacter); } } |
Result
1 2 3 4 | String:Java String charAt() example Character at first position: J Character at sixth position: S Character at last position: e |
Now Print all characters with its index value
Example
1 2 3 4 5 6 7 8 9 | public class StringChatAtExample1 { public static void main(String args[]) { String test = "Java String charAt() example"; for(int i=0;i<=test.length()-1;i++){ System.out.println("Character at index value "+i+" = "+test.charAt(i)); } } } |
Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Character at index value 0 = J Character at index value 1 = a Character at index value 2 = v Character at index value 3 = a Character at index value 4 = Character at index value 5 = S Character at index value 6 = t Character at index value 7 = r Character at index value 8 = i Character at index value 9 = n Character at index value 10 = g Character at index value 11 = Character at index value 12 = c Character at index value 13 = h Character at index value 14 = a Character at index value 15 = r Character at index value 16 = A Character at index value 17 = t Character at index value 18 = ( Character at index value 19 = ) Character at index value 20 = Character at index value 21 = e Character at index value 22 = x Character at index value 23 = a Character at index value 24 = m Character at index value 25 = p Character at index value 26 = l Character at index value 27 = e |
Java String Method compareToIgnoreCase()
This method is used to compare two string with case ignorance and compare both string lexicographically. If both strings are same then it returns 0. If first string is greater then another then it returns positive integer, if second string is greater then first then it returns negative integer value.
Signature
public int compareToIgnoreCase(String str)
Parameters
str – String value that will compared with string.
Return
Integer value. if string is greater then passed str value then it returns positive integer if both strings are equal then it returns zero if passed str is greater then string then this method returns negative integer value.
Example1
1 2 3 4 5 6 7 8 9 10 11 | public class StringCompare1 { public static void main(String[] args) { System.out.println("Comparing A with A " + "A".compareToIgnoreCase("A")); System.out.println("Comparing A with b " + "A".compareToIgnoreCase("b")); System.out.println("Comparing C with b " + "C".compareToIgnoreCase("b")); System.out.println("Comparing A with Z " + "A".compareToIgnoreCase("Z")); System.out.println("Comparing Z with A " + "Z".compareToIgnoreCase("A")); } } |
Result
1 2 3 4 5 | Comparing A with A 0 Comparing A with b -1 Comparing C with b 1 Comparing A with Z -25 Comparing Z with A 25 |
In above program comparing same string is returning 0. In second println() statement A is lexicographically smaller then b so it is returning -1. In third println() statement C is lexicographically greater then b so it is returning 1 and so on.
Example2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class StringCompare { public static void main(String[] args) { String first = "Hello"; String second = "Hell"; String third = "Bye"; String fourth = "hello"; String fifth = "Hello"; String sixth = "xyz"; int result1 = first.compareToIgnoreCase(second); System.out.println("result1 " + result1); int result2 = first.compareToIgnoreCase(third); System.out.println("result2 " + result2); int result3 = first.compareToIgnoreCase(fourth); System.out.println("result3 " + result3); int result4 = first.compareToIgnoreCase(fifth); System.out.println("result4 " + result4); int result5 = first.compareToIgnoreCase(sixth); System.out.println("result5 " + result5); } } |
Result
1 2 3 4 5 | result1 1 result2 6 result3 0 result4 0 result5 -16 |
Java String Method indexOf with int parameter
indexOf(int ch) method is used to get the first occurrence of a character in given string. This method takes a character input (in Unicode code units) and return the index value of character in string, if character not found in string then it returns -1.
Signature
public int indexOf(int ch)
Parameters
A charcter value in unicode code point.
Return
Returns the int value that represents the first occurance of the character in string, if character not fount in string then it returns -1.
Example
1 2 3 4 5 6 7 8 9 10 11 12 | public class StringIndexOf1 { public static void main(String[] args) { String string = "Java is a Object Oriented Programming Language"; System.out.println("Index of Character J is " + string.indexOf('J')); System.out.println("Index of Character J is " + string.indexOf(0x4A)); System.out.println("Index of Character O is " + string.indexOf('O')); System.out.println("Index of Character e is " + string.indexOf('e')); System.out.println("Index of Character E is " + string.indexOf('E')); System.out.println("Index of Character z is " + string.indexOf('z')); } } |
1 2 3 4 5 6 | Index of Character J is 0 Index of Character J is 0 Index of Character O is 10 Index of Character e is 13 Index of Character E is -1 Index of Character z is -1 |
Java String Method indexOf()
indexOf method is used to get the first occurrence of a sub string in given string. This method takes a string input and return the index value of it in given string object, if sub string not found in string then it returns -1.
Signature
public int indexOf(String str)
Parameters
str is sub string value to find in specified string.
Return
Returns the int value that represents the first occurrence of the sub string in string, if sub string not fount in string then it returns -1.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class StringIndexOfExample { public static void main(String[] args) { String text = "World is beautiful"; System.out.println("Index of w is " + text.indexOf("w")); System.out.println("Index of W is " + text.indexOf("W")); System.out.println("Index of i is " + text.indexOf("i")); System.out.println("Index of l is " + text.indexOf("l")); System.out.println("Index of world is " + text.indexOf("world")); System.out.println("Index of World is " + text.indexOf("World")); System.out.println("Index of is is " + text.indexOf("is")); System.out.println("Index of beautiful is " + text.indexOf("beautiful")); } } |
Result
1 2 3 4 5 6 7 8 | Index of w is -1 Index of W is 0 Index of i is 6 Index of l is 3 Index of world is -1 Index of World is 0 Index of is is 6 Index of beautiful is 9 |
Java String Method lastIndexOf()
This method take a string parameter as input and return the last index value of parameter string in specified string. If string not found then it returns -1.
The second overloaded method find the string from fromindex position.
Signature
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
Parameters
str- to search in String
fromindex- to search str from specified index
Return
Return the last index value of parameter str in specified string. If str not found then it returns -1
Example
1 2 3 4 5 6 7 8 9 10 11 | public class StringLastIndexOf { public static void main(String[] args) { String s = "People who practice a lot get higher scores."; System.out.println("Last index of a :" + s.lastIndexOf("a")); System.out.println("Last index of J :" + s.lastIndexOf("J")); System.out.println("Last index of s :" + s.lastIndexOf("s")); System.out.println("Last index of lot :" + s.lastIndexOf("lot")); } } |
Result
1 2 3 4 | Last index of a :20 Last index of J :-1 Last index of s :42 Last index of lot :22 |
Java String substring() method
This method takes a int argument to generate sub string of string. It returns sub string starting from beginIndex value to last of string.
The second overloaded method allows to specify the ending index value also.
beginIndex parameters is inclusive and endIndex is exclusive.
Signature
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
Parameters
beginIndex –generate substring from this index value(inclusive).
endIndex-generate substring up to endIndex-1 value(exclusive).
Return
A substring
Throw
IndexOutOfBoundsException: If beginIndex is negative or endIndex is greater then length of string or if beginIndex is greater then endIndex.
Example
1 2 3 4 5 6 7 8 9 10 11 12 | public class StringSubString { public static void main(String[] args) { String str = "It is very simple to be happy"; System.out.println("Substring1 : " + str.substring(0)); System.out.println("Substring2 : " + str.substring(6)); System.out.println("Substring3 : " + str.substring(18)); System.out.println("Substring4 : " + str.substring(24)); System.out.println("Substring5 : " + str.substring(0, 17)); System.out.println("Substring6 : " + str.substring(21, str.length())); } } |
Result
1 2 3 4 5 6 | Substring1 : It is very simple to be happy Substring2 : very simple to be happy Substring3 : to be happy Substring4 : happy Substring5 : It is very simple Substring6 : be happy |
Java String concat() method
String concat() is used to concatenate two strings. The parameter str string is appended to specified string and new string object is returned by this method.
Example
“Hello ”.concat(“Friends”)
This will generate a new string object “Hello Friends”
Parameters
Str – is string parameter used to concatenate with specified string
Example
1 2 3 4 5 6 7 8 9 | public class StringConcat { public static void main(String[] args) { String str = "Hello Friends"; String str1 = "How are you"; String str3 = str.concat(" ").concat(str1); System.out.println(str3); } } |
1 | Hello Friends How are you |
Here str is concatenated with ” ” then it is concatenated with str1 and result is assigned to str3 String object.
Read More