String Class

Author: Ravi Poswal

1. equals( ):-this method is used for variable content comparison.

Syntax:-  public boolean equals(Object);

Example: String s1 = "core";
String s2 = "Java";
System.out.println(s1.equals(s2)); // true

2. Length() :- It is used to find out a length of the string
Syntax:- public int length();

Example: String str = "Hello Java";
int len = str.length();  // 10

3. compareTo():-it is based on how each character in the strings is represented in Unicode. Return 0 if the strings are identical. Return a negative integer if the first string's lexical length is shorter than the second string's and a positive number if the first string's length is longer.

Syntax:- int compareTo(String);

Example:

String s1 = "Apple";
String s2 = "Banana";
String s3 = "Apple";
System.out.println(s1.compareTo(s2)); // negative
System.out.println(s2.compareTo(s1)); // positive
System.out.println(s1.compareTo(s3)); // zero

4. startsWith():-It is checking the prefix of a String. It is the Boolean return value, i.e., true or false. Syntax:-  public boolean startsWith(String);

Syntax:- public boolean startsWith(String, int);

Example:-   System.out.println("Ranrev Infotech".startsWith("Ranrev")); =>true

                    System.out.println("Ranrev Infotech".startsWith("Infotech")); =>false

5. endsWith():- It is checking the suffix of a String, i.e., String end check.

Syntax:- public boolean endsWith(String);

Example:- System.out.println("Ranrev Infotech".endsWith("Infotech")); =>true

System.out.println("Ranrev Infotech".endsWith("Ranrev")); =>false

6. indexOf(int)& lastIndexOf(int, int):- It determines the start and end index positions for the character value or substring provided.

Syntax: public int indexOf(String);     

Example:   String str = "Java Programming";

        int index1 = str.indexOf('a');
        int index2 = str.lastIndexOf("Pro");

        System.out.println(index1);  // first 'a'
        System.out.println(index2);  // "Pro"

7. substring(int):- this is used to obtain part of the string.

Syntax: public String substring(int);

Example:- String s="abcdabcabd";

System.out.println(s.substring(2,4)); =>cd

System.out.println(s.substring(5)); =>bcabd

8. concat():- this is used for combining two strings.

Syntax :-public String concat(String)

Example:-      String s= "Ravi";

System.out.println(s);  =>Ravi

String t= s.concat("Indian");

System.out.println(t);  =>RaviIndian

9. toLowerCase()&toUpperCase():-this is used to change the cases you want.

Syntax:- public String toLowerCase()

  Public String toUpperCase()

Example 1: 

String s="I LOVE MY INDIA";

System.out.println(s.toLowerCase()); =>i love my India

Example 2:

  String s="i love my india";

System.out.println(s.toUpperCase()); =>I LOVE MY INDIA

10. valueOf(primitive type) :-this is used for convert primitive(int, double, float, short, byte, char, boolean, long ) value to string.

Syntax:-public static String valueOf(primitive type )

Example:          int a=8;

char b='r';

double d=12.45;

String s1= String.valueOf(a);

String s2= String.valueOf(b);

String s3= String.valueOf(d);

System.out.println(s1); => 8

System.out.println(s2); => r

System.out.println(s3); => 12.45