Tuesday, 24 September 2013

Java String | Selenium Uses

Java String methods examples based on the string,
String a = "This is some copy welcoming the user to this section and explaining what they can expect to find here.";

1|  substring()
Truncates text till string length 24
String Result = a.substring(24);
_
ing the user to this section and explaining what they can expect to find here.

Truncates text after string length 24
String Result = a.substring(0, 24);
_
This is some copy welcom


2|  toUpperCase()
Converts string to Uppercase
String Result = a.toUpperCase();
_
THIS IS SOME COPY WELCOMING THE USER TO THIS SECTION AND EXPLAINING WHAT THEY CAN EXPECT TO FIND HERE.


3|  toLowerCase()
Converts string to Lowercase
String Result = a.toLowerCase();
_
this is some copy welcoming the user to this section and explaining what they can expect to find here.


4|  concat()
Combines two different strings
String Result = a.concat("Adding this text");
_
This is some copy welcoming the user to this section and explaining what they can expect to find here.Adding this text


5|  replace()
Replace a string with another
String Result = a.replace("some", "few");
_
This is few copy welcoming the user to this section and explaining what they can expect to find here.


6|  replaceFirst()
Replaces the first word from the string
String Result = a.replaceFirst("to", "ot");
_
This is some copy welcoming the user ot this section and explaining what they can expect to find here.


7|  length()
Finds the length of the string
int Result = a.length();
System.out.println(Result);
_
102


8|  charAt()
Finds the letter/char at length location 0
char Result = a.charAt(0);
System.out.println(Result);
_
T


9|  contains()
Checks the specific text in the string
boolean Result = a.contains("some");
System.out.println(Result);
_
true


10|  contentEquals()
verifies that the content matches the string in boolean
boolean Result = a.contentEquals("This is some copy welcoming the user to this section and explaining what they can expect to find here.");
System.out.println(Result);
_
true


11|  endsWith()
verifies that the end of the sentence matches the string in boolean
boolean Result = a.endsWith("expect to find here.");
System.out.println(Result);
_
true


12|  startsWith()
verifies that the start of the sentence matches the string in boolean
boolean Result = a.startsWith("This is some");
System.out.println(Result);
_
true


13|  equals()
verifies that the text matches the string in boolean
boolean Result = a.equals("This is some copy welcoming the user to this section and explaining what they can expect to find here.");
System.out.println(Result);
_
true


14|  equalsIgnoreCase()
verifies the string by ignoring case-sensitive characters in boolean
boolean Result = a.equalsIgnoreCase("THIS IS SOME COPY WELCOMING THE USER TO THIS SECTION AND EXPLAINING WHAT THEY CAN EXPECT TO FIND HERE.");
System.out.println(Result);
_
true


15|  isEmpty()
verifies the string is empty in boolean
boolean Result = a.isEmpty();
System.out.println(Result);
_
false


17|  matches()
verifies that the text matches string in boolean
boolean Result = a.matches("This is some copy welcoming the user to this section and explaining what they can expect to find here.");
System.out.println(Result);
_
true


18|  getBytes()
Encode String
byte[] Result = a.getBytes();
System.out.println(Result);
Decode String
String s = new String(Result);
System.out.println("Text Decrypted : " + s);
_
[B@10deb5f
Text Decrypted : This is some copy welcoming the user to this section and explaining what they can expect to find here.


19|  indexOf()
Finds the index using Character of the string
System.out.print("Found Index : " );
System.out.println(a.indexOf( 'T' ));
System.out.println(a.indexOf( 'h' ));
System.out.println(a.indexOf( 'i' ));
System.out.println(a.indexOf( 's' ));
_
Found Index : 0
1
2
3


20|  compareTo()
Returns 0 for True and 1 for False after string comparison
int Result = a.compareTo("dfg");
System.out.println(Result);
int Result1 = a.compareTo("This is some copy welcoming the user to this section and explaining what they can expect to find here.");
System.out.println(Result1);
_
1 // False
0 // True


21|  compareToIgnoreCase()
Returns 0 for True and 1 for False after string comparison that ignores case-sensitive
String b = "THIS IS SOME COPY WELCOMING THE USER TO THIS SECTION AND EXPLAINING WHAT THEY CAN EXPECT TO FIND HERE.";
int Result = a.compareToIgnoreCase(b);
System.out.println(Result);
_
// True

2 comments: