Java String: 8 examples of simple and string functions
The strings in Java
The Java programming language comes up with the String class that can be used for creating strings in programs.
The strings are widely used in programming languages that are a combination of characters. As the String is a class in Java, you have to create objects for creating any string. Before going into further details, have a look at how you may create strings in Java.
Before going into further details, have a look at how you may create strings in Java.
An example of creating a simple string
A string can be created just like primitive type variables in the Java program as follows:
Code for creating a simple string:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class String_demo { public static void main(String []args) { String Str_demo = "This is a Java String tutorial!"; // creating a simple string System.out.println(Str_demo); } } |
Output:
So, a simple string can be created without using the new keyword:
String Str_demo = “A string example”;
As this code executes and compiler finds any string literal, it creates a string object with its value.
Creating string with new keyword example
As such, String is the class; just like creating other objects in Java programming, you may create a string object by using the new keyword and constructor as follows:
The code for creating a string with new keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class String_demo { public static void main(String []args) { String Str_demo = new String(); Str_demo = "The string class has 40 methods!"; System.out.println(Str_demo); } } |
Result:
An example of creating string from an array
You may use difference sources for creating the strings. In above examples, I created a string that is enclosed in double quotes. You may create a string from an array or other sources. See an example below:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class String_demo { public static void main(String []args) { char[] charArr = { 'S', 'a', 'y', ' ', 'h', 'e','l','l','o' }; String Str_demo = new String(charArr); System.out.println(Str_demo); } } |
Output:
Main points about Java strings
- The string is the sequence of characters.
- The String is a class in Java.
- The string class is immutable. That means a string object cannot be changed after it is created.
- The number of constructors of string class is eleven.
- The string class has plenty of useful functions. A few are demonstrated below.
A demo of using the string length function
The string length method can be used for getting the total number of characters in the given string object. See this example where I have created the following string object:
String Str_demo = “The string in Java is immutable”;
See the code and output for getting the length of that string in Java program:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class String_demo { public static void main(String []args) { String Str_demo = "The string in Java is immutable"; // creating a simple string int str_len = Str_demo.length(); System.out.println("The length of the string is = " + str_len); } } |
Output:
A demo of using the concat method
The strings can be concatenated by using the string class method concat(). You may concatenate string objects including string literals. Have a look at this example where three strings are created. First is the string object. The second is a string object with a value. The third string is the concatenated string of other two strings along with its own value:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class String_demo { public static void main(String []args) { String Str_1 = "series of characters and "; // creating a simple string String Str_2 = new String(); String Str_3 = new String(); Str_2 = "immutable in Java."; Str_3 = "The Strings are " .concat(Str_1) .concat(Str_2); System.out.println(Str_3); } } |
Output:
You may also use the + for concatenating strings, as used in many of the examples in the series of Java. The + not only allows concatenating strings but you may also combine other objects as well.
A demo of using the replace method of Strings
The string replace method can be used for replacing the given characters with new characters in the given string.
See this example where the following string is created “The java string is an object.”
By using the replace method, the ‘j’ is replaced by capital ‘J’:
The string replace method code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class String_demo { public static void main(String []args) { String Strex = "The java string is an object."; // creating simple System.out.println("Actual String = " + Strex); System.out.println("After using replace method: " + Strex.replace('j', 'J')); } } |
Output:
An example of using the string split method
The split method of String class returns an array of broken string. The split method breaks the string by the given word or regular expression. For example, you may specify “and” “or” etc. to break the string.
See this example where I have broken the string by specifying the “and” word. As the split method returns an array, the for loop is used to display the returned array elements:
The split method code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class String_demo { public static void main(String []args) { String StrSplit; StrSplit ="Java string class has 40 methods and among those is a split method."; for (String Strbroken: StrSplit.split(" and ")){ System.out.println(Strbroken); } } } |
Output:
A demo of using the format method of String class
For string formatting, you may use the format method of String class. You may also use the printf for formatting the string. The advantage of using the format function is you may reuse the formatted string.
In the following example, the format method is used where %d formatting character is used that will replace the integer values in the given string:
The code for formatting the string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class String_demo { public static void main(String []args) { int int_method; int int_constructors; int_method = 40; int_constructors= 11; String formatted_String; formatted_String =String.format("The String class has %d methods and %d constructors" ,int_method,int_constructors); System.out.println(formatted_String); } } |
Output:
There are other useful methods in String class like substring, equals, indexof, compare etc.