Automation Using Selenium Webdriver
Showing posts with label in java. Show all posts
Showing posts with label in java. Show all posts

Friday 21 October 2016

Compare Strings Examples in java


package com.methods;
public class String_Functions {
public static void main(String[] args) {
String Str1="iam learning selenium";
String str2="   iam lazy to practise";
//equals function
System.out.println("comapring two styrings::"+Str1.equals(str2));
//concat function
System.out.println("Concatenation two strings:::"+Str1.concat(str2));
//charAt function
String st1="iam so beautifull";
System.out.println("index of charaAT:::"+st1.charAt(5));
//length function
System.out.println("lenght of st1::"+st1.length());
//toLowerCase function
String s="sELeNIuM";
System.out.println("LOWERCASE::"+s.toLowerCase());
//toUpperCase function
String s1="SEleIuM";
System.out.println("LOWERCASE::"+s1.toUpperCase());
//indexOf function
String t="iam very happy";
System.out.println("index of:::"+t.indexOf("very"));
//valueOf function
int j=100;
System.out.println("convert int to string::"+String.valueOf(j));
//parseInt function
String i="200";
System.out.println("convert string to int::"+Integer.parseInt(i));
//substring function
String st="i want job without practise";
System.out.println("substring is:::"+st.substring(11, 18));
//substring function
String p="RS/-300/-";
System.out.println("price substring is:::"+p.substring(4,7));
//split function
String splt="HCL@INFOSYS@DELOITE@IBM@GOOGLE@TECH";
String arrayvar[]=splt.split("@");
for(String each:arrayvar){
System.out.println("each index::"+each);
}
//split function
String sn="i want job in top mnc but i will not be regualr to class";
String var[]=sn.split(" ");
for(String eachindex:var){
System.out.println("index is::"+eachindex);
}
//trim function
String tr="                    testing tools                ";
System.out.println("trim vallue is::"+tr.trim());

}

}

Comparing Strings:
In order to compare Strings for equality, you should use the String object's equals or equalsIgnoreCase
 methods.

For example, the following snippet will determine if the two instances of String are equal on all
 characters
String firstString = "Test123";
String secondString = "Test" + 123;
if (firstString.equals(secondString)) {
   // Both Strings have the same content.
}
This example will compare them, independent of their case


import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
            public static void main (String[] args) throws java.lang.Exception
            {
                        String firstString = "Test123";
                        String secondString = "TEST123";
                        if (firstString.equalsIgnoreCase(secondString)) {
                           System.out.print("Equal");
                        }
            }
}

Comparing Strings in switch statement

String stringToSwitch = "A";
switch (stringToSwitch) {
    case "a":
        System.out.println("a");
        break;
    case "A":
        System.out.println("A"); //the code goes here
        break;
    case "B":
        System.out.println("B");
        break;
    default:
        break;
}

Changing the case of characters within a String


The String type provides two methods for converting strings between upper case and lower case:

toUpperCase to converts all characters to upper case
toLowerCase to convert all characters to lower case
These methods both return the converted strings as new String instances:
the original String objects are not modified.
String string = "This is a Random String";
String upper = string.toUpperCase();
String lower = string.toLowerCase();

System.out.println(string);   // prints "This is a Random String"
System.out.println(lower);    // prints "this is a random string"
System.out.println(upper);    // prints "THIS IS A RANDOM STRING"


Finding a String Within Another String

To check whether a particular String a is being contained in a String b or not, we can use the method
 String.contains() with the following syntax:

b.contains(a); // Return true if a is contained in b, false otherwise
The String.contains() method can be used to verify if a CharSequence can be found in the String.
The method looks for the String a in the String b in a case-sensitive way.

String str1 = "Hello World";
String str2 = "Hello";
String str3 = "helLO";

System.out.println(str1.contains(str2)); //prints true
System.out.println(str1.contains(str3)); //prints false


Reversing Strings

There are a couple ways you can reverse a string to make it backwards.

StringBuilder/StringBuffer:

String code = "code";
 System.out.println(code);

 StringBuilder sb = new StringBuilder(code);
 code = sb.reverse().toString();
Char array:
String code = "code";
System.out.println(code);

char[] array = code.toCharArray();
for (int index = 0, mirroredIndex = array.length - 1; index < mirroredIndex; index++, mirroredIndex--) {
    char temp = array[index];
    array[index] = array[mirroredIndex];
    array[mirroredIndex] = temp;
}

// print reversed
System.out.println(new String(array));

 System.out.println(code);