Hit enter after type your search item

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:


Output:

string

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:


Result:

string new

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:


Output:

string from array

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:


Output:

string length

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:


Output:

string concat

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:


Output:

string replace

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:


Output:

string split

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:


Output:

string format

There are other useful methods in String class like substring, equals, indexof, compare etc.

This div height required for enabling the sticky sidebar