Java Strings

October 21, 2018 Off By ravi24kant

Introduction to Java Strings

Java Strings are sequences of characters. In Java the strings have been implemented like objects. This has been done so that operations can be performed on strings. Operations such as adding or comparing 2 strings etc. You can perform on strings. String is a class. You can also create normal string variables and create an object of string class too. And can perform operations with both.

String myString = “this is a string”; // variable
String myString = new String(“This is a string”); // object

String is the same as a variable and used like an object. When you use the string as an object, once the object is created, you can not make any changes to it. For example, look at the example above, I created an object named
myString. But now its text “This is a string” can not be changed. “But when you use the string as a variable, it can be easily changed. Java provides some methods, from which you can do the modification in the string object. But whenever you do this, a new string object is generated. There is no change in the original string object. All these operations are being  given below.

String Length

The number of characters in any string is the length of that string. You can use length () method to know the length of any string. For this you call length method call on that string.

class String_demo
{
public static void main(String[] arg)
{

String myString = “hello”;
System.out.println(myString.length());

       }

}

Concatenating Strings

When a third string is created by adding two strings, then it is called string concatenation. You can do this also through the + operator and also by concat () method.

Examples of these two methods are being given below.

class String_demo
{
public static void main(String[] arg)
{

String myString = “This is a string”;
String yourString = “This is your string”;
String ourString = myString+yourString; // with + operator
ourString = myString.concat(yourString); // with concat() method
System.out.println(ourString);

       }

}

String Indexing with charAt()

These methods are used to extract one of the method strings. Strings are similar to indexing arrays. The first character is on 0 index. So you can access any single character of the string. For this, you use the charAt () method. The index number that you pass in this method returns this method to the character of the same index.

class String_demo
{
public static void main(String[] arg)
{

String myString = “hello”;
System.out.println(myString.charAt(3));

     }

}

String Indexing with substring()

You use substring () method to search sub string in a string. In this method, you pass 2 arguments. The first argument is that index number where you want to access the string. And the second argument is that index number is whatever string you want to access. This method returns the string from start index to ending index.

class String_demo
{
public static void main(String[] arg)
{

String s1 = “hello my name is Verma”;
System.out.println(s1.substring(5,8));

     }

}

String Indexing with indexOf()

To find out which sub string is the index, you use the indexOf () method. In
this method a sub string pass is made. This method returns the stating index
of that sub string.

class String_demo
{
public static void main(String[] arg)
{

String s1 = “hello my name is Ravi”;
System.out.println(s1.indexOf(“my”)); //returns 7

   }

}

String Comparison with equals()

Equals () method is used to compare any two strings. If both strings are equal then this method returns true. This method is called on a string and passes like a second string argument. An example of this is being given below.

class String_demo
{
public static void main(String[] arg)
{

String s1 = “hello Ravi”;
String s2 = “hello Ravi”;

System.out.println(s1.equals(s2)); // returns true

    }

}

class String_demo
{
public static void main(String[] arg)
{

String s1 = “hello Ravi”;
String s2 = “hello ravi”;

System.out.println(s1.equals(s2)); // returns false ; String is Case sensitive

    }

}