Home » Java Tutorial » Learn String operations in Java using examples

Learn String operations in Java using examples

In the previous article, we discussed the String class in Java. We learnt the ways a String object can be constructed. We also figured out the difference between String and StringBuffer. We discussed 2 string methods length() and Intern() as well. I hope that the article would have been helped you in the endeavours learning Strings in Java.

If you are preparing for any automation related interview then you should read Commonly asked java programs in Selenium Interview
and you should read this post which covers the most common interview question related to String

5 Commonly Asked Java String Question in Selenium Interview

In continuation here we will talk about some more string operation (mostly useful for automation testing practice) through which String can be manipulated in the many ways according to the need. In automation testing, we face many situations where it is required to manipulate the string.

  1. Concatenating String: In Java, two or more string objects can be concatenated in 2 ways as below:
    1. Using + operator: Please take a look in the below program:


      //another example


       
    2. Using concat() method: Please refer to below example:

       
  2. CharAt: This method helps us to find the character at the given index of the String. The index starts from 0 i.e. the first character of the string is at the 0th index. The last character is at the length() – 1 index. (Syntax: public char charAt(int index) )

  3. compareTo: This method is used to compare the two strings lexicographically. It returns int value as the comparison result.
    Value ‘0’ is returned if both the strings are equal. Value less than ‘0’ is returned if the argument string is greater than this string. A value greater than ‘0’ is returned if the argument string is less than this string.

    CompareTo method can also be used to compare any 2 objects as this method is part of Java object class. So it is not limited to String comparisons only.
  4. ContentEquals: This method compares the String with StringBuffer and returns a Boolean value.

     

     

  5. Equals and equalsIgnoreCase: To compare two strings for equality, use equals( ). It has this general form:

    Here, str is the String object is being compared with the invoked String object. It returns true if the strings contain the same characters in the same order, and false otherwise. The comparison is case-sensitive. To perform a comparison that ignores case differences, call equalsIgnoreCase().

    When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:

    Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise.
    Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):

    The output from the program is shown here:

     

  6. getChars: If you need to extract more than one character at a time, you can use the getChars( ) method. It has this general form:

    Here is the output of this program:

    [the_ad_placement id=”incontent”]

     

  7. replace: The replace( ) method replaces all occurrences of one character in the invoking string with another character. It has the following general form:

    Here, original specifies the character to be replaced by the character specified by replacement. The resulting string is returned. For example,

    puts the string “Hewwo” into s.

  8. split:  Ahhh!! I just love this method. I have built a framework around this function only.
    You can split a String using this method. The string is broken around the given delimiter(regular expression). It returns an array of Strings. Let’s understand by an example:

    Here is the output of this program:

    Number of splited Strings can also be controlled by using the overloaded method of Split 

    output:

     

    [the_ad_placement id=”incontent”]

  9. substring: You can extract a substring using substring( ). It has two forms. The first is

    Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string. The second form of substring( ) allows you to specify both the beginning and ending index of the substring:

    Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.

    output:

     

  10.  toUppercase and toLowerCase: These 2 methods are used to convert the questioned string to all upper case letters and lower case letters respectively.

     

     

  11. Trim: If there is a situation where you have the string which has spaces in its starting and in ending, this method will help you out to remove those.

If you want to explore more about String then read it from here

Read Similar Posts



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.