String Handling in Java

String Handling in Java

String is sequence of characters written in double quotes.

I) Java String Example: 

package javaExamples;

class StringExample {
public ststic void main (String [] args) {

System.out.println ("Hello Selenium"); // Hello Selenium is a String
System.out.println ("123 Hello Selenium");
System.out.println ("Hello Selenium 123");
System.out.println ("123*456 Hello Selenium");
}
}
Note: String may contain Alfa bytes, numbers and special characters.
Creating Strings

> String is considered as object in java.

II) Example for Creating Strings:
package javaExamples;

public class StringExample {
   
public static void main (String []args){
    String myTool = "Selenium"; // String Variable

    String [] myTools = {"UFT", "Selenium", "LoadRunner", "Quality Center"}; // Array of Strings.

    System.out.println(myTool);// Selenium

    for (int i=0; i < myTools.length; i++) {
        System.out.println(myTools[i]); // Print Array of strings   
        }
    }
}
--------------------------------------
III) Concatenating Strings
package javaExamples;

public class StringExample {
   
public static void main (String []args){
    String str1 = "Selenium"; // String Variable
    String str2 = "Testing";

    System.out.println(str1 + str2);// Selenium Testing
    System.out.println("Test Automation " + "Using Selenium" + " and Java"); // Test Automation using Selenium and java
    System.out.println(1 + 1 + " Selenium");
    System.out.println("Selenium" +1 + 1);
        }
}
-------------------------------
IV) String Comparison in Java:
a) String comparison using equals() method

b) String comparison using == (Relational Operator)

c) String comparison using compareTo() method

Example:

package javaExamples;

public class StringExample {
   
public static void main (String []args){
    String str1 = "SELENIUM";
    String str2 = "selenium";
    String str3 = "SELENIUM";
    String str4 = "selenium testing";
   
    System.out.println(str1.equals(str2));// false
    System.out.println(str1.equals(str3));// true
   
    System.out.println(str1 == str2); // false
    System.out.println(str1 == str3); // true
}
}
--------------------------
V) Important operations on Strings:
 

package javaExamples;

public class StringExample {
   
public static void main (String []args){
       String str1 = "Selenium";
       String str2 ="gcreddy@gmail.com";
      
       System.out.println(str1.length());// 8 (finding length of the String)
     
       System.out.println(str1.contains("len"));// true (finding sub string)
       System.out.println(str1.contains("lem"));// false (finding sub string)
      

      // Returning Sub Strings
      
System.out.println(str2.substring(0)); // gcreddy@gmail.com
       System.out.println(str2.substring(9)); // gmail.com
       System.out.println(str2.substring(14)); // com
       System.out.println(str2.substring(9, 13)); // gmail
    }
}
-----------------------------------------

Also Read:



Java Variables

Java Operators 

Java Conditional Statements 

Java Loop Statements 

Java Arrays 

Java Methods

0 comments:

Post a Comment