Pages

String Programming

String is a sequence of characters (or) group of characters (or) collection of characters. String is a Final Class and String is a immutable class which means a constant and cannot be changed once created. String is available in java.lang package and Strings can declared as objects and variables.

String are two ways to create String object

1. Creating a String Using Literal.

String str1 = "Delhi";
String str2 = "Mumbai";
String str3 = "Chennai";
String str4 = "Bangalore";
String str5 = "Jaladhi Naga Vijay";
String str6 = "Jaladhi Soft Technology";
2. Creating a String Using Object

String str1 = new String("New York");
String str2 = new String("Los Angeles");
String str3 = new String("Chicago");
String str1 = new String("Pennsylvania");
String str2 = new String("California");
String str3 = new String("San Jose");

String Interview Questions

1. String Logical Question
String s1 = " ";
String s2 = null;
What is a output of s1.length() statement ?
Ans : It's display Zero;
What is a output of s2.length() statement ?
Ans : It's display Null Pointer Exception;

2. String Logical Question
String a = "abc";
String b = new String("abc");
What is a output of a.equals(b) statement ?
Ans : True
What is a output of a == b statement ?
Ans : False

3. String Logical Question
String a = new String("abc");
String b = new String("abc");
What is a output of a.equals(b) statement ?
Ans : True
What is a output of a == b statement ?
Ans : False

4. String Logical Question
String a = "abc";
String b = "abc";
What is a output of a.equals(b) statement ?
Ans : True
What is a output of a == b statement ?
Ans : True

5. String Logical Question
What is the output of below statement ?
String st = "What is the String ? ";
String [] a = st.split("\s");
Ans : Compile Time Error

6. String Logical Question
What is the output of below statement ?
String st = "What is the String ? ";
String [] a = st.split("\\s");
String [] a = st.split(" ");
Ans : Both statements are same.
Both are working perfectly that means Compile
and Run the Statements.
7. String Logical Question
What is the output of below if condition ? if(null == null) {
System.out.println("Output");
}
Ans : Compile and Run. Display the Output
What is the output of below if condition ?
int i = null; ---> Compile Time Error.
Integer in = null; ---> Compile and Run.
8. String Logical Question
What is the output of below if condition ?
String a = null;
if(a.equals(null)) {
System.out.println("Output");
}
Ans : Compile and Throw Run Time Exception.
9. Integer Logical Question
What is the output of below statement ?
int x = 07;
Ans : Compile and Run
int x = 08;
Ans : Compile Time Error
Reason : The Literal 08 of type int is out of range.

1. Basic String Example

public Class StringOne {
      String a = "Hello"; // String Literal.
      String b = new String("Welcome") // String Object.
      public void display() {
            System.out.println(" String Literal : " + a);
            System.out.println(" String Object : " + b);
      }
      public static void main(String [] args) {
            StringOne so = new StringOne();
            so.display();
      }
}

String Literal : Hello
String Object : Welcome
String Methods :

String charAt() Method : String charAt() method returns the character located at the String's specified index. The string indexes start from zero.
String charAt( ) Method using String Literal
public class CharAtMethod {
      public static void main(String args[]) {
            String s = "Jaladhi Soft Technology";
            char result1 = s.charAt(1);
            char result2 = s.charAt(8);
            char result3 = s.charAt(10);
            System.out.println(result1);
            System.out.println(result2);
            System.out.println(result3);
      }
}

a
s
f

String charAt() Method using String Object
public class CharAtMethod {
      public static void main(String args[]) {
            String s = new String("Jaladhi Soft Technology");
            char result1 = s.charAt(1);
            char result2 = s.charAt(8);
            char result3 = s.charAt(10);
            System.out.println(result1);
            System.out.println(result2);
            System.out.println(result3);
      }
}

a
s
f

String length( ) Method : String length() method returns the number of characters contained in the string object.
String Length Method using Litarial
public class String Litarial {
      public static void main(String[] args) {
            String str = "Jaladhi Soft Technology";
            System.out.print("String Length Method : " + str.length());
      }
}

String Length Method : 23
String Length Method using Object
public class StringObject {
      public static void main(String[] args) {
            String str = new String("Jaladhi Naga Vijay");
            System.out.print("String Length Mehod : "str.length());
      }
}

String Length Method : 18
String Length Method using Litarial with For Loop
public class StringLength {
      public static void main(String[] args) {
            String str = "Jaladhi";
            for(int i = 0; i < str.length(); i++) {
                  char result = str.charAt(i);
                  System.out.print(i + " ");
            }
      }
}

0 1 2 3 4 5 6
String Length Method using Object with For Loop
public class StringLength {
      public static void main(String[] args) {
            String str = new String("New York");
            for(int i = 0; i < str.length(); i++) {
                  char result = str.charAt(i);
                  System.out.print(i + " ");
            }
      }
}

0 1 2 3 4 5 6
2. String Concatenation Method : Add a string to the end of another string (or) Combine a string to the end of another string. public Class StringTwo {
      String a = "Hello"; // String Literal.
      String b = new String("Welcome") // String Object.
      public void display() {
            a = a.conat(b);
            System.out.println(" String Literal : " + a);
      }
      public static void main(String [] args) {
            StringTwo so = new StringTwo();
            so.display();
      }
}

String Literal : Hello Welcome

3. String Length Method Example

public Class StringThree {
      String a = "Hello"; // String Literal.
      String b = new String("Welcome") // String Object.
      public void display() {
            int b = a.length();
            int c = b.length();
            System.out.println(" String Literal Length : " + b);
            System.out.println(" String Object Length : " + c);
      }
      public static void main(String [] args) {
            StringThree so = new StringThree();
            so.display();
      }
}

String Literal Length : 5
String Object Length : 7

4. String charAt Method using without for Example

public Class StringFour {
      String a = "Hello"; // String Literal.
      String b = new String("Welcome") // String Object.
      public void display() {
            String c = a.charAt(3);
            String d = b.charAt(4);
            System.out.println(" String Literal : " + c);
            System.out.println(" String Object : " + d);
      }
      public static void main(String [] args) {
            StringFour so = new StringFour();
            so.display();
      }
}

String Literal Length : 5
String Literal : l
String Object : o

5. String charAt Method using with for Example

public Class StringFive {
      String a = "Hello"; // String Literal.
      String b = new String("Welcome") // String Object.
      public void display() {
            // String Literal
            for(int i = 0; i < a.length(); i++) {
                  char c = a.charAt(i);
                  System.out.print(c);
            }
            // String Object
            for(int i = 0; i < b.length(); i++) {
                  char d = a.charAt(i);
                  System.out.print(d);
            }
      }
      public static void main(String [] args) {
            StringFive so = new StringFive();
            so.display();
      }
}

H e l l o
W e l c o m e

6. String comparTo Method Example

public Class StringSix {
      String a = "Hello";
      String b = "Welcome";
      String c = "One";
      String d = "hello";
      public void display () {
            System.out.println(a.compareTo(b));
            System.out.println(a.compareTo(c));
            System.out.println(a.compareTo(d));
            System.out.println(b.compareTo(b));
            System.out.println(c.compareTo(c));
            System.out.println(d.compareTo(a));
      }       public static void main(String [] args) {
            StringSix ss = new StringSix();
            ss.display();
      }
}

-15     -7     -32     0     0     32

7. String comparToIgnoreCase Method Example

public Class StringSeven {
      String a = "Hello";
      String b = "Welcome";
      String c = "One";
      String d = "hello";
      public void display () {
            System.out.println(a.compareToIgnoreCase(b));
            System.out.println(a.compareToIgnoreCase(c));
            System.out.println(a.compareToIgnoreCase(d));
            System.out.println(b.compareToIgnoreCase(b));
            System.out.println(c.compareToIgnoreCase(c));
            System.out.println(d.compareToIgnoreCase(a));
      }       public static void main(String [] args) {
            StringSeven ss = new StringSeven();
            ss.display();
      }
}

-15     -7     0     0     0     0

8. String equals Method Example

public Class StringEight {
      String a = "Hello";
      String b = "Welcome";
      String c = "One";
      String d = "hello";
      public void display () {
            System.out.println(a.equals(b));
            System.out.println(a.equals(c));
            System.out.println(a.equals(d));
            System.out.println(b.equals(b));
            System.out.println(c.equals(c));
            System.out.println(d.equals(a));
      }       public static void main(String [] args) {
            StringEight ss = new StringEight();
            ss.display();
      }
}

False     False     False    True     True     False

9. String equalsIgnoreCase Method Example

public Class StringNine {
      String a = "Hello";
      String b = "Welcome";
      String c = "One";
      String d = "hello";
      public void display () {
            System.out.println(a.equalsIgnoreCase(b));
            System.out.println(a.equalsIgnoreCase(c));
            System.out.println(a.equalsIgnoreCase(d));
            System.out.println(b.equalsIgnoreCase(b));
            System.out.println(c.equalsIgnoreCase(c));
            System.out.println(d.equalsIgnoreCase(a));
      }
      public static void main(String [] args) {
            StringNine ss = new StringNine();
            ss.display();
      }
}

False     False     True     True     True     True

10. String subString Method Example

public Class StringTen {
      String a = "Hello ! How are you? ";
      public void display () {
            // subString single parameter
            System.out.println(a.subString(6));
            System.out.println(a.subString(7));
            // subString two parameters
            System.out.println(a.subString(0,9));
            System.out.println(a.subString(10,19));
      }
      public static void main(String [] args) {
            StringTen ss = new StringTen();
            ss.display();
      }
}

How are you?
How are you?
Hello !
How are you?

11. String toLowerCase Method Example

public Class StringEleven {
      String a = "Hello ! How are you? ";
      String b = "I am Fine.";
      public void display () {
            System.out.println(a.toLowerCase());
            System.out.println(b.toLowerCase());
      }       public static void main(String [] args) {
            StringEleven ss = new StringEleven();
            ss.display();
      }
}

hello ! how are you?
i am fine.

12. String toUppCase Method Example

public Class StringTweleven {
      String a = "Hello ! How are you? ";
      String b = "I am Fine.";
      public void display () {
            System.out.println(a.toUppCase());
            System.out.println(b.toUppCase());
      }
      public static void main(String [] args) {
            StringTweleven ss = new StringTweleven();
            ss.display();
      }
}

HELLO ! HOW ARE YOU?
I AM FINE.

13. String trim Method Example

public Class StringThirteen {
      String a = "      Hello ! How are you?       ";
      String b = "      I am Fine.       ";
      public void display () {
            System.out.println(a.trim());
            System.out.println(b.trim());
      }
      public static void main(String [] args) {
            StringThirteen ss = new StringThirteen();
            ss.display();
      }
}

Hello ! How are you?
I am Fine.

15. String contentEquals Method Example

public class StringFourteen {
      public static void main(String args[]) {
            String str1 = "One";
            String str2 = "Two";
            StringBuffer str3 = new StringBuffer("One");
            boolean result = str1.contentEquals(str3);
            System.out.println(result);
            result = str2.contentEquals(str3);
            System.out.println(result);
      }
}

Returned Value = true
true       false

16. String copyValueOf Method Example

public class StringFifteen {
      public static void main(String args[]) {
            char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
            String b = "";
            b = b.copyValueOf(a);
            System.out.println("Returned String: " + b);
            b = b.copyValueOf( a, 2, 6 );
            System.out.println("Returned String: " + b);
      } }

Returned Value = true
Returned String: hello world
Returned String: llo wo

17. String endsWith Method Example

public class StringSixteen {
      public static void main(String args[]){
            String a = new String("Java String Programming ");
            boolean b;
            b = a.endsWith( "Programming" );
            System.out.println("Returned Value = " + b);
            b = a.endsWith( "Prog" );
            System.out.println("Returned Value = " + b);
      }
}

Returned Value = true
Returned Value = false

18. String getBytes Method Example

public class StringSeventeen {
      public static void main(String [ ] args) {
            String a = new String("String Programming are very important in Java.");
            try {
                  byte[] b = Str1.getBytes();
                  System.out.println("Returned Value " + b);
                  b = a.getBytes( "UTF-8" );
                  System.out.println("Returned Value " + b);
                  b = a.getBytes( "ISO-8859-1" );
                  System.out.println("Returned Value " + b);
            } catch(Exception e) {
                  System.out.println("Unsupported character set");
            }
      }
}

Returned Value [B@192d342
Returned Value [B@15ff48b
Returned Value [B@1b90b39

19. String hashCode Method Example

public class StringEighteen {
      public static void main(String [ ] args) {
            String Str = new String("String Programming are very important in Java.");
            System.out.println("Hashcode for Str :" + Str.hashCode() );
      }
}

Hashcode for Str :1186874997

20. String indexOf Method Example

public class StringNineteen {
      public static void main(String [ ] args) {
            String a = new String("Java String Programming.");
            String b = new String("Programming");
            String c = new String("Grogramming");
            System.out.print("Found Index :" );
            System.out.println(a.indexOf('o'));
            System.out.print("Found Index :" );
            System.out.println(a.indexOf('o', 5 ));
            System.out.print("Found Index :" );
            System.out.println(a.indexOf(b));
            System.out.print("Found Index :" );
            System.out.println(a.indexOf(b, 15 ));
            System.out.print("Found Index :" );
            System.out.println(a.indexOf(c));
      }
}

Found Index : 4
Found Index :9
Found Index :11
Found Index :-1
Found Index :-1

21. String intern Method Example

public class StringTwenty {
      public static void main(String [ ] args) {
            String a = new String("Java Language");
            String b = new String("JAVA LANGUAGE");
            System.out.print("Canonical representation:" );
            System.out.println(a.intern());
            System.out.print("Canonical representation:" );
            System.out.println(b.intern());
      }
}

Canonical representation: Java Language
Canonical representation: JAVA LANGUAGE