String Operations in Java

1.9K views 5 minutes read
A+A-
Reset

Class String provide no of methods to perform string operations in java. We already discussed about ways of creating Strings. In java whenever compiler get a string literal it treats it as an string objects. String literals are any thing enclosed in double quotes. In java string operations are one of the most used thing. Lets discuss some of very frequent and important string operations f java.

1. Finding length of string:

Class String provided a method called length() to find the number of characters of the string.

Signature:

public int length();

Example: 

String s = "araj"; // since java create String object for every string literals
System.out.println("String s = " +s);
System.out.println("Length of String s is " +s.length());
System.out.println("Length of string "araj" is " +"araj".length());

/////////////////////////////////////////////////////////////////////////
//                   ////*Output*////                                  //
//  String s = araj                                                    //
//  Length of string s is 4                                            //
//  Length of string araj is 4                                         //
/////////////////////////////////////////////////////////////////////////

One thing we should notice is that we can call method length by direct with string literal. This shows that string literals are objects in java.

2. Concatenation

  •    We can concatenate strings by using concatenate (+) operator. A new string is formed after inserting second string following first string. We can directly print the string or assign it to another reference.                      Example String Operations  1:
    String s = "This is our long string so " +
    	   "concatenation helps."+
    	   ".Long statements make code ugly";
    System.out.println(s);
    int x =15;
    String s = "My age is ";
    System.out.println(s+x); // concatenating  string and integer
    System.out.println("I am " + 20 + 2);
    System.out.println("I am "+(20+2));
    ///////////////////////////////////////////////////////////////////////////////////
    //                            //** Output **//                                   //
    // This is our long string so concatenation helps..Long statements make code ugly//
    // My age is 15                                                                  //
    // I am 202                                                                      //
    // I am 22                                                                       //
    ///////////////////////////////////////////////////////////////////////////////////

    Generally + operator helps to manage long strings. + operator is most frequently used inside print statement. We can concatenate the different data types with strings. As we concatenated integer with string. Here whenever compiler encounter string as operand of + operator it automatically convert another operand into equivalent string and perform concatenation. Compiler uses toString() method to convert any other datatype into string. Last two statements in the example shows that arithmetic operations are better to enclosed in parenthesis to avoid the undesired output.

  • We also can concatenate the string using concat() method of class String.                                                     Signature:                              public String concat(String);                                                                                     Example String Operations 2:
    String x ="15";
    String s = "My age is ";           ///// Here are equivalent code 
    String s1 = s.concat(x);           //
    String s2 = x.concat(s);           //
    String s3 = "Ram".concat(" "+x);   //
    System.out.println(s1);            //System.out.println(s.concat(x));
    System.out.println(s2);            //System.out.println(x.concat(s));
    System.out.println(s3);            //System.out.println("Ram".concat(" "+x));
    ////////////////////////////////////////////////////////////////////////////////
    //                     //*Output*//                                           //
    // My age is 15                                                               //
    // 15My age is                                                                //
    // Ram 15                                                                     //
    ////////////////////////////////////////////////////////////////////////////////

    The concat() method adds argument string after calling string and return as new string. We can pass an string reference or string literal in this method.

3. String Comparison

Strings can be compared by following methods.

  • equals() method: this method take a string as argument and compare the order and values of characters inside the string. It returns boolean value if number and order of character is same, otherwise false.
  • equals to (==)operator: This method compares address rather than values. If two string  references are pointing to the same string object it will return true, otherwise false.
  • compareTo() method : This method compares string in lexicographical way and return an integer representing difference between strings.

Example of String Operations 2:

class StringTest{
private void equalsToOperator(String s1, String s2){
if(s1==s2){
    System.out.println("Both points to same object so, The == operator yields " + (s1==s2));
}else{
    System.out.println("Both do not point to same object so The == operator yields " + (s1==s2));
} 
}
private void equalsToMethod(String s1, String s2){
if (s1.equals(s2)){
     System.out.println("""+s1+"" have same contents ""+s2+"" ,so this function return " + 
                    s1.equals(s2));
}else{
     System.out.println("""+s1+"" do not have same content ""+s2+"" so this function return "+ 
                     s1.equals(s2));
}

}
private void compareToMethod(String s1, String s2){
int i = s1.compareTo(s2);
if(i==0){
   System.out.println("Both strings have same content so difference is " + s1.compareTo(s2));
}else if(i>0){
    System.out.println("""+s1+"" alphabetically precedes ""+s2+"" so this function return " + 
s1.compareTo(s2));
}else{
   System.out.println("""+s2+"" string alphabetically precedes ""+s1+"" so this function return " +
 s1.compareTo(s2));
}

}

public static void main(String[] args){


String x ="Sultan";
String s = "Sultan";
String s1 = "Raees";
StringTest t1 =new StringTest();
t1.equalsToOperator(x,s);
t1.equalsToMethod(x,s);
t1.compareToMethod(x,s1);
t1.compareToMethod(x,s);

}
}

output:

String Operations

Both points to same object so, The == operator yields true
"Sultan" have same contents "Sultan" ,so this function return true
"Raees" string alphabetically precedes "Sultan" so this function return 1
Both strings have same content so difference is 0

please Leave comments in case of confusion….

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.