Introduction to String - The Coding Shala
Home >> Data Structures >> Introduction to String
Introduction to String
A String is a collection of Unicode characters and similar to the arrays. We can perform almost all the operations on the string that we used in an array.
Below is some function that we use frequently with Strings.
Compare Function
The String has its own compare function, unlike the == operator. We can compare two strings in the following ways:
- we can use == operator, like string1 == string2.
- we can use equals() method. like string1.equals(string).
- we can use compareTo() method. For example string1.compareTo(string2).
The Java String compareTo() method is used for comparing two string lexicographically. If both the strings are the same then this method returns 0 else it returns a positive or negative value. It returns a positive value if the first string is lexicographically greater than the second string else returns negative.
The following example explains the compare function:
//String Example public class Main{ public static void main(String[] args) { //initializing string String str1 = "Hello World"; System.out.println(str1); String str2 = str1; //refer to the same object String str3 = new String(str1); //another object with same value System.out.println("Compare with == :"); System.out.println("str1 and str2 is: "+ (str1==str2)); System.out.println("str1 and str3 is: "+ (str1==str3)); System.out.println("Compare with equals() method: "); System.out.println("str1 and str2 is: "+ (str1.equals(str2))); System.out.println("str1 and str3 is: "+ (str1.equals(str3))); System.out.println("Compare with compareTo() method:"); System.out.println("str1 and str2 is: "+ (str1.compareTo(str2)==0)); //0 for same System.out.println("str1 and str3 is: "+ (str1.compareTo(str3)==0)); } }
Output:
Hello World Compare with == : str1 and str2 is: true str1 and str3 is: false Compare with equals() method: str1 and str2 is: true str1 and str3 is: true Compare with compareTo() method: str1 and str2 is: true str1 and str3 is: true
In Java, Strings are immutable which means we can't change the content of the string once it's initialized.
The following example explains the basic operation on Strings:
//String Example public class Main{ public static void main(String[] args) { //initializing string String str1 = "Hell"; //str1[0] = 'F'; this will give error since strings are immutable //concat on string str1 = str1 + 'o'; System.out.println(str1); str1 = str1 + " World"; System.out.println(str1); //get char by index System.out.println("Char at index 1 is: "+ str1.charAt(1)); //index of a char System.out.println("index of o char is: "+ str1.indexOf('o')); System.out.println("Last index of char o is: "+ str1.lastIndexOf('o')); //substring System.out.println(str1.substring(6)); System.out.println(str1.substring(1,5)); } }
Output:
Hello Hello World Char at index 1 is: e index of o char is: 4 Last index of char o is: 7 World ello
We know in Java, Strings are immutable so if we want to work with strings either we can convert string to char Array or can use another data structure like StringBuilder. The following example explains it:
//String Example public class Main{ public static void main(String[] args) { //initializing string String str1 = "Fello"; //str1[0] = 'F'; this will give error since strings are immutable System.out.println(str1); char[] st = str1.toCharArray(); st[0] = 'H'; System.out.println(st); //String Builder example StringBuilder sb = new StringBuilder(); for(int i=0;i<str1.length();i++) { sb.append(str1.charAt(i)); } System.out.println(sb.toString()); System.out.println(sb.reverse()); } }
Output:
Fello Hello Fello olleF
Other Posts You May Like
Comments
Post a Comment