java string – How to use string java class with example

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.



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.



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’.



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

Was this article helpful?

Related Articles

Leave A Comment?