String manipulation

String Manipulation

The String class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks.

As String is a non primitive data type so when we declare and initialize a string variable and  if we give a dot(.) 

after string variable there will be show a lot of method name.

Length Method

As we have said before there are some methods in String class which are very helpful to execute a java program according to our requirement. Now we will discuss some of them.

Length Method:

The length method shows the length of  the String. The length of the sequence of characters represented by this object.
Such as:
    
public class StringManupulation {
    
    public static void main(String[] args) {
    
        String name;
    
        name = "maria";
    
        int lengthofcharacter = name.length();
    
        System.out.println("length of character is " + lengthofcharacter);
    
       }
    
}

Output:

Index Of Method:

This method helps to  determine the location of a specific character that you specify.

The index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

example:

public class StringManupulation {

    public static void main(String[] args) {
        String name;
        name = "maria";
        int indexOfi = name.indexOf('r');
        System.out.println("index of char r is " + indexOfi);
    }

}

Output:

CharAt Method

CharAt() Method:

This method  Returns the char value at the specified index. An index ranges from 0 to length() – 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for  array indexing.

example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name = "maria";
        char i = name.charAt(4);
        System.out.println("4 no charter is of this String is " + i);
        }
}

Output:

Concat Method

Concat() Method:

By using this method we can join two strings in Java.

Example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name="maria";
        String newName = name.concat(" Alam");
        System.out.println("after concat " + newName);
        }
}

Output:

SubString Method

SubString() Method:

To show the a part of a specified string we use a Substring()method. This method Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name = "maria";
        String a = name.substring(2);
        System.out.println("A part of name maria is: " + a);
        }
}

Output:

StartsWith Method

StartsWith() Method:

To test if the string starts with the specified character or not  we use Startwith() method.This method returns a boolean value.

Example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name = "maria";
        boolean v = name.startsWith("i");
        System.out.println(v);
       }
}

Output:

Replace Method

Replace() Method:

If we want to change a character of a string we use replace method.

This method returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

Example:

public class StManupulation_exm {
    public static void main(String[] args) {
        String name;
        name="Java,Python,c++,PHP";

                String v=name.replace(" , " , "_");
        System.out.println(v);
     }
}

Output:

CompareTo Method

CompareTo() Method:

This method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.
If all the characters are same then it returns 0. Otherwise it returns the value on the basis of Unicode value. This method is case sensitive.
Example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name = "maria";                      // case         sensitive
        System.out.println("Result after Comparing the characters:"+name.compareTo("Maria"));
 
         }
}

Output:

If we want to ignore lower case and upper case difference we can use compareToIgnoreCase method.
Example:

public class StringManupulation {
    public static void main(String[] args) {
        String name;
        name = "maria";// ignore case
        System.out.println("Result after Comparing the characters: "+name.compareToIgnoreCase("Maria"));
        }
}

Output:

IsBlank Method

IsBlank() Method:

If we want to check if the string is blank or not we use IsBlank() method. It returns a Boolean value. It Returns true if the string is empty or contains only white space codepoints, otherwise it returns false.

Example:

public class StringManupulation {
       public static void main(String[] args) {
        String b = " ";
        boolean s = b.isBlank();
        System.out.println("The result is: "+s);
         }    
}

Output:

IsEmpty Method

IsEmpty() Method:

This method is used to check if the String is empty or not. It returns  true if, and only if, length() is 0.Otherwise it returns false.

Example:

public class StringManupulation {
    public static void main(String[] args) {
        String b = " ";
        boolean  e = b.isEmpty();
        System.out.println("The result is: "+e);
        }
}

Output:

Trim Method

Trim() Method:

The trim() method eliminates whitespace before and after the string. 

This method returns 

a string whose value is this string, with all leading and trailing space removed, or this string if it has no leading or trailing space.

 Example:

public class StManupulation_exm {
    public static void main(String[] args) {
        String address = "  256 Irving,Dallas,Texas";
                System.out.println("before trim:-" + address);
        System.out.println("after trim:-" + address.trim());
       }
}

Output:

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close