Java String
- The Java platform gives the String class to create and manage strings.
- Strings are a series of characters.
- Java String class gives a variety of strategies to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() and many others.
- String is an immutable item this means that it is a constant and might can not be modified as soon as it’s been created.
String Declaration:
1 | String greeting ="Hello world!"; |
Example:
1 2 3 4 5 6 7 8 9 10 | package package_Java; public class String_Java { public static void main(String args[]) { String str1 = new String("Hello Sandeep"); System.out.println(str1); } } |
Output:
1 | Hello Sandeep |
String Methods Explanation:
- int length(): Returns the number of characters in the String.
- Char charAt(int i): Returns the character at ith index.
- String substring (int i): go back the substring from the ith index person to cease.
- String substring (int i, int j): Returns the substring from i to j-1 index.
- String concat( String str): Concatenates precise string to the stop of this string.
- int indexOf (String s): Returns the index inside the string of the first occurrence of the desired string.
- int indexOf (String s, int i): Returns the index inside the string of the first prevalence of the specified string, starting at the required index.
- boolean equals( item otherObj): Compares this string to the required object.
- boolean equalsIgnoreCase (String anotherString): Compares string to every other string, ignoring case concerns.
- int compareTo( String anotherString): Compares string lexicographically.
- int compareToIgnoreCase( String anotherString): Compares string lexicographically, ignoring case considerations.
- String toLowerCase(): Converts all of the characters within the String to decrease case.
- String toUpperCase(): Converts all of the characters in the String to upper case.
- String trim(): Returns the reproduction of the String, by disposing of whitespaces at each ends. It does no longer affectwhitespaces inside the center.
- String replace (char oldChar, char newChar): Returns new string by replacing all occurrences of oldChar with newChar.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 | //converts the string s1 to all lowercase s2=s1.toLowerCase; //converts the string s1 to all Uppercase s2=s1.toUpperCase; //Replace all occurance of x with y s2=s1.replace('x','y'); //Removes the white spaces at the beginning and end of the String s1. s2=s1.trim() //Returns true if s1 is equal to s2 s1.equals(s2) //Returns true if s1=s2, ignoring the case of characters. s1.equalsIgnoreCase(s2) //Gives the Length of s1 s1.length() //Gives nth character of s1 s1.charAt(n) //negative if s1<s2,positive if s1>s2, and zero if s1 is equal s2 s1.compareTo(s2) //concatenates s1 and s2 s1.concat(s2) //Gives substring starting from nth character s1.substring(n) //Gives substring starting from nth charater upto mth s1.substring(n,m) //Gives the position of the first occurence of 'x' in the string s1. s1.indexOf('x') //position of the 'x' that occurs after nth position the string s1. s1.indexOf('x','n') |
Example:
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package package_Java; public class String_Java { public static void main(String[] args) { String st1 = "India is Very Nice"; String st2 = " And Beautiful."; //Comparing two strings. Return true If both match else false. System.out.println("st1 equals to st2? -> " + st1.equals(st2)); // Concatenates st2 with st1. System.out.println("Concatenation of st1 and st2 Is -> " + st1.concat(st2)); // Retrieve the 9th Indexed character from string. System.out.println("Character at Index 9 Is -> " + st1.charAt(9)); // Find the length of string. System.out.println("Length Of St1 -> " + st1.length()); // Converting whole string In lower case. System.out.println("String In Lowercase -> " + st1.toLowerCase()); // Converting whole string In upper case. System.out.println("String In uppercase -> " + st1.toUpperCase()); // Retrieve the Index of first 'i' character. System.out.println("Index of 1st charater i Is -> " + st1.indexOf('i')); // Retrieve the index of 2nd most 'i' character. System.out.println("Index of 2nd charater i Is -> " + st1.indexOf('i', 3)); // Retrieve the Index of word 'Very' from string. System.out.println("Index of word Very Is -> " + st1.indexOf("Very")); // Converting value From int to string. int j = 75; String val2 = String.valueOf(j); System.out.println("Value Of string val2 Is -> " + val2); // Converting string to integer. String val1 = "50"; int i = Integer.parseInt(val1); System.out.println("Value Of int i Is -> " + i); // Print the String starting from 5th Index to 12th Index. System.out.println("Retrieving sub string from string -> " + st1.substring(5, 13)); // Split string. String splt[] = st1.split("Very"); System.out.println("String Part 1 Is -> " + splt[0]); System.out.println("String Part 2 Is -> " + splt[1]); // Trim String. System.out.println("Trimmed st2 -> " + st2.trim()); } } |
Output:
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 29 | st1 equals to st2? -> false Concatenation of st1 and st2 Is -> India is Very Nice And Beautiful. Character at Index 9 Is -> V Length Of St1 -> 18 String In Lowercase -> india is very nice String In uppercase -> INDIA IS VERY NICE Index of 1st charater i Is -> 3 Index of 2nd charater i Is -> 3 Index of word Very Is -> 9 Value Of string val2 Is -> 75 Value Of int i Is -> 50 Retrieving sub string from string -> is Very String Part 1 Is -> India is String Part 2 Is -> Nice Trimmed st2 -> And Beautiful. |