Quick Reach
String in Java
Strings are combination of characters that are widely used in java programming. For example “Strings are supported in Java programming”.
Java comes up with String class to work with strings. So strings are treated as objects in Java. A string object once created cannot be changed i.e. string class is immutable.
A few main points about Strings in java:
- String in java is object with sequence of characters
- The class String has forty methods
- String class has 11 constructors
- String objects are immutable, once created it cannot be changed
- The methods provided by String class are very useful in performing strings actions like string comparison, searching substring, changing case of letters, concatenation/joining strings etc.
A few method’s examples are shown below
Creating a simple java string
A string can be created simply as follows:
String strex = “Java comes up with string class”;
This is simple way of creating strings. In that case as string literal is executed in complier it will create String object.
You can also create strings just like other objects in java. See below:
String strex = new String();
strex = “Java comes up with string class”;
That is using new keyword to create class instance of string.
Example of creating string in java
The example below creates two strings, one simple and other uses new keyword.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class string_example {
public static void main(String []args) {
String Strex = "Strings are supported in java by using String class"; // creating simple
String strex1 = new String(); //creating with new keyword
strex1 = "Java comes up with string class";
System.out.println(Strex);
System.out.println(strex1);
}
}
|
See graphic of above example
The output will be:
Strings are supported in java by using String class
Java comes up with string class
A few common methods of String class
There are around 40 utility methods in String class like string comparison, string length, splitting string etc. A few commonly used are given below along with examples. List of more methods can be found at bottom of this chapter along with chapter link.
String length method
The syntax of string length method is:
Strex.length()
The length() method of string returns length or total number of characters in string.
Example of using java string length method
This example displays length of two strings by using java length method of string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class string_example {
public static void main(String []args) {
String Strex = "Strings are supported in java by using String class"; // creating simple
String strex1 = new String(); //creating with new keyword
strex1 = "Java comes up with string class";
System.out.println("String length=" + Strex.length());
System.out.println("String length=" + strex1.length());
}
}
|
See graphic of above example
The output will be:
String length=51
String length=31
String replace method
The syntax of string replace method is:
Strex.replace()
The replace() method of string replaces existing characters to new given characters.
Example of using java string replace method
This example replaced small ‘c’ letter in existing string to ‘C’.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class string_example {
public static void main(String []args) {
String Strex = "Strings are supported in java by using String class"; // creating simple
Strex = "Java comes up with string class";
System.out.println("The actual string: " + Strex);
System.out.println("String after replace: " + Strex.replace('c', 'C'));
}
}
|
See graphic of above example
The output will be:
The actual string: Java comes up with string class
String after replace: Java Comes up with string Class
List of common String methods
A few commonly used string class method with links to their respective chapters.
– String length method
– String concat method
– String format
– String split method
– String replace method
– String substring method
– String trim method
Leave A Comment?