What is a string?
In C, C++, a string represents an array of characters (last character ends with ‘\0’).
In Java, a string represents the object of String class. We also have character array in java.
Is String a class or a datatype?
String is a class defined in java.lang package. In Java, every class is considered as user defined data type. So we can take string as a data type.
Ways to create a String:
Method 1: By assigning a group of characters to a string type variable.
Syntax:
String <variable_name>;
<varaible_name> = “string”; |
Example:
String name;
name = “Hari”;
Declaration and assignment can be made by a single statement. This is just a simplification of the above.
Syntax:
String <variable_name> = value; |
Example:
String name = “Hari”;
Method 2: Creating an object to string class by allocating memory using new operator.
Syntax:
String <object> = new String(“value”); |
Example:
String name = new String(“Hari”);
Method 3: Converting the character array into strings.
Syntax:
String <object> = new String(<Char_Array>); |
Example:
char n[] = {‘H’,’a’,’r’,’i’};
String name = new String(n);
Note: A string can be empty with no characters inside it.
Example:
String s=””
Concatenating strings:
The concat() method is used for concatenating two strings.
Example:
String s1 = “freduu”;
String s2 = “ java tutorial”;
String s = s1.concat(s2); //variable s contains “freduu java tutorial”
Strings can also be concatenated with the help of + operator.
Example:
String s = s1+s2;
Note: Concatenating a variable of int type with an empty string would convert the integer into String.
Example: int i = 2;
String s =””+i; // converts i to string
However this method is considered to be a bad practice.
String.valueOf(int) or char.toString() is preferred for conversion.
Comparing strings:
The equals() method of the String class is used to compare the value of two strings. If the content of two strings is same, then the equals() method, returns true. Otherwise as false.
Example:
String s1 = “Today we are feeling blue”;
String s2 = “Today we are feeling blue”;
Boolean equality = s1.equals(s2); // equality would contain true
Equals method is case sensitive. Suppose if we do not want to consider the case of the strings while comparing, then we could use equalsIgnorecase() method.
Example:
String str1 = “Java”;
String str2 = “java”;
Boolean comp = str1.equalsIgnoreCase(str2); // comp would contain true
There is another method for comparing strings which is compareTo() method. This method compares the strings lexicographically, which checks the unicode value corresponding to every character.
If str1 <str2 , returns -ve number
If str1>str2, returns +ve number
If both strings are equal, 0 is returned. |
The numbers returned are the difference of the character values (unicode value) at which both the strings differ. The compareTo() method is used in sorting problems, where we need to sort a set of strings in an alphabetical order like the name of employee in an employer list.
Example:
1
2
3
4
5
6
7 |
String s1 = ”java”;
String s2 = “java”;
String s3 = “jazz”;
String s4 = “jassy;
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3)); // s1 precedes s3 since z comes after v
System.out.println(s1.compareTo(s4)); // s1 follows after s4 since s comes before v |
Output:
Extracting substrings:
The substring() method can be used to extract sequence of characters (substring) from a given string. There are variations in using the substring method.
Syntax :
String substring(int p1, int p2) |
This method extracts the characters starting from the position p1 to p2-1.
Example:
String s1 = “Airport”
String s = s1.substring(3,7); // s would contain “port”
Syntax:
String substring(int p)
This method extracts the characters starting from the position p till the end of the string.
Example:
String s1 = “Airport”
String s = s1.substring(3); // s would contain “port”
Finding Length of the String;
The length() method returns the number of characters in the String.
Example:
String s =”Air”
int length = s.length(); // length would have 3,
To know more Java String visit
Java String
Java Buffer
Java Builder