String In Java - Tutorials For WebDriver

We have learnt about DIFFERENT DATA TYPES IN JAVA In my past post. Now, Many peoples are understanding that Java String Is also a data type. Let me correct them -> String Is not data type in java software development language. If string Is not data types then what Is String? This question can be asked by Interviewer too. Strings are objects prepared by sequence of characters. The java.lang has String class to create and manipulate strings. String class has many Inbuilt functions which we can use to perform different actions on string.

If you don't know, Let me tell you that we have to work with strings very frequently In selenium WebDriver tests for software application. So you must have knowledge of useful functions of String class to use them In your selenium webdriver software test case development. Let us take simple example of String to understand different methods of String class in java software development language.

public class Strng_Example {

 public static void main(String[] args) {
  
  String st1 = "This World is Very Nice";
  String st2 = " And Beautiful.";
  
  //Comparing two strings. Return true If both match else return false.
  System.out.println("st1 equals to st2? -> "+st1.equals(st2));
  
  //Concatenates st2 with st1.
  System.out.println("Concatenation of st1 and st2 Is -> "+st1.concat(st2));
  
  //Retrieve the 9th Indexed character from string.
  System.out.println("Character at Index 9 Is -> "+st1.charAt(9));
  
  //Find the length of string.
  System.out.println("Length Of St1 -> "+st1.length());
  
  //Converting whole string In lower case.
  System.out.println("String In Lowercase -> "+st1.toLowerCase());
  
  //Converting whole string In upper case.
  System.out.println("String In uppercase -> "+st1.toUpperCase());
  
  //Retrieve the Index of first 'i' character.
  System.out.println("Index of 1st charater i Is -> "+st1.indexOf('i'));
  
  //Retrieve the index of 2nd most 'i' character.
  System.out.println("Index of 2nd charater i Is -> "+st1.indexOf('i', 3));
  
  //Retrieve the Index of word 'Very' from string.
  System.out.println("Index of word Very Is -> "+st1.indexOf("Very"));
  
  //Converting value From int to string.
  int j = 75;
  String val2 = String.valueOf(j);
  System.out.println("Value Of string val2 Is -> "+val2);
  
  //Converting string to integer.
  String val1="50";
  int i = Integer.parseInt(val1);
  System.out.println("Value Of int i Is -> "+i);
  
  //Print the String starting from 5th Index to 12th Index.
  System.out.println("Retrieving sub string from string -> "+st1.substring(5, 13));
  
  //Split string. 
  String splt[] = st1.split("Very");
  System.out.println("String Part 1 Is -> "+splt[0]);
  System.out.println("String Part 2 Is -> "+splt[1]); 

                //Trim String.
  System.out.println("Trimmed st2 -> "+st2.trim()); 
 }
}

If you will look at above example, I have prepared different examples of string method to take some action or we can say operations on string. Sort explanation of all above string methods are as bellow.

Two String Comparison
To compare two strings, we can use syntax like st1.equals(st2). It will return True If both strings are same else It will return False.

Two String Concatenation
Syntax st1.concat(st2) will concatenate st1 with st2.

Retrieving Character from Index
Syntax st1.charAt(9)) will retrieve the character from string st1 located at 9th Index. Index Starts from 0.

Length Of String
st1.length() will return lenght of string st1.

Convert String In Lower Case Letters
st1.toLowerCase() will convert whole string letters In lower case.

Convert String In UpperCase Letters
st1.toUpperCase() will convert whole string letters In upper case.

Retrieving Index Of 1st most character
st1.indexOf('i') will retrieve Index of first most character 'i' from string.

Retrieving Index Of 2nd most character
st1.indexOf('i', 3)) will retrieve Index of second most character 'i' from string. 3 described the from Index means It will start finding character 'i' from Index 3. First 'i' has Index 2 so we need to use 3 to find 2nd most character.

Retrieving Index Of specific word from string
st1.indexOf("Very") will retrieve index of word 'Very' from string.

Convert from Integer To String
String.valueOf(j) will convert value of 'j' from int to string.

Convert from String To Integer
Integer.parseInt(val1) will conver value of 'val1' from string to int.

Retrieving sub string from string
Syntax st1.substring(5, 13) will retrieve string from Index 5 To Index 13.

Split String
String splt[] = st1.split("Very") will split string st1 from word 'Very' and store both strings In array.

Trim String
If string has white space at beginning or end of the string then you can use trim function like st2.trim() to remove that white space.

20 comments:

  1. Nice posts, but i think a next and prev button to switch among the tutorials would have been more helpful for readers

    ReplyDelete
  2. Great Article, extremely helpful for newbies like myself

    ReplyDelete
  3. It would be more helpful if you post the output for the above mentioned program.

    ReplyDelete
    Replies
    1. st1 equals to st2? -> false
      Concatenation of st1 and st2 Is -> This World is Very Nice And Beautiful.
      Character at Index 9 Is -> d
      Length Of St1 -> 23
      String In Lowercase -> this world is very nice
      String In uppercase -> THIS WORLD IS VERY NICE
      Index of 1st charater i Is -> 2
      Index of 2nd charater i Is -> 11
      Index of word Very Is -> 14
      Value Of string val2 Is -> 75
      Value Of int i Is -> 50
      Retrieving sub string from string -> World is
      String Part 1 Is -> This World is
      String Part 2 Is -> Nice
      Trimmed st2 -> And Beautiful.

      Delete
    2. In the split function I can not see word very in the out put ?!

      Delete
    3. I can not see the word very in the output from the split function !

      Delete
  4. I have my code as follows

    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    //Thread.sleep(20000);
    driver.switchTo().frame("EntryFrame");
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    String TotalbalanceSTR=driver.findElement(leaveRequestPage.txtannual).getText();
    int TotalbalanceINT=Integer.parseInt(TotalbalanceSTR);
    System.out.println("Value of total balance->"+TotalbalanceINT);
    report.updateTestLog("Verify bank is updated correctly", "Leave bank updated successfully", Status.PASS);

    --> first line is like am switching from parent window to child window and waiting to get load for elements
    -->then i have derived a string and stored into a variable and converted to integer
    -->but system output is not printing the integer and got error as @For input string: "150.00"@

    ReplyDelete
    Replies
    1. I think your value is double not integer so you need to convert it in double as bellow
      double Totalbalanced = Double.parseDouble(TotalbalanceSTR);

      Delete
  5. Thanks Aravind. But i have a question, while switching from parent to child window, to load the "String TotalbalanceSTR" value it takes around 20 sec (depends on the performance of application).

    Before loading the value my code is executing and printing the value as "empty String" . How can i handle this?

    i have used implicitWait if u see my code but i guess it wont work

    ReplyDelete
  6. Hi Meghasri,

    Have you tried with Explicit Wait like below example
    ex:
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput"

    ????

    ReplyDelete
  7. The article is good, please add the links to Next and Previous tutors please.

    ReplyDelete
  8. thank you very much to me that I am a beginner this is cool

    ReplyDelete
  9. Content can not be any better than this..Thanks a lot :)

    ReplyDelete
  10. So usefull.Thank you

    ReplyDelete
  11. Post is very useful and depth but need to display output as per define code. So, beginner level person can understand easily.

    ReplyDelete
  12. Please post along with the output

    ReplyDelete
  13. "character" written without second "c" )

    ReplyDelete