STRING METHODS

It is a predefined class in java.lang package can be used to handle the String. String class isimmutable that means whose content can not be changed at the time of execution of program.
String class object is immutable that means when we create an object of String class it never changes in the existing object.
Example:

Example

class StringHandling
{
public static void main(String arg[])
{
String s=new String("java"); 
s.concat("software");
System.out.println(s);
}
}

Output

java
Explanation: Here we can not change the object of String class so output is only java not java software.

Methods of String class

length()

length(): This method is used to get the number of character of any string.

Example

class StringHandling
{
public static void main(String arg[])
{
int l;
String s=new String("Java"); 
l=s.length();
System.out.println("Length: "+l);
}
}

Output

Length: 4

charAt(index)

charAt(): This method is used to get the character at a given index value.

Example

class StringHandling
{
public static void main(String arg[])
{
char c;
String s=new String("Java"); 
c=s.charAt(2);
System.out.println("Character: "+c);
}
}

Output

Character: v

toUpperCase()

toUpperCase(): This method is use to convert lower case string into upper case.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java"; 
System.out.println("String: "+s.toUpperCase());
}
}

Output

String: JAVA

toLowerCase()

toLowerCase(): This method is used to convert lower case string into upper case.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="JAVA"; 
System.out.println("String: "+s.toLowerCase());
}
}

Output

String: java

concat()

concat(): This method is used to combined two string.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy"; 
System.out.println("Combined String: "+s1.concat(s2));
}
}

Output

Combined String: HiteshRaddy

equals()

equals(): This method is used to compare two strings, It return true if strings are same otherwise return false. It is case sensitive method.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy"; 
String s3="Hitesh";
System.out.println("Compare String: "+s1.equals(s2));
System.out.println("Compare String: "+s1.equals(s3));
}
}

Output

Compare String: false
Compare String: true

equalsIgnoreCase()

equalsIgnoreCase(): This method is case insensitive method, It return true if the contents of both strings are same otherwise false.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="HITESH"; 
String s3="Raddy";
System.out.println("Compare String: "+s1.equalsIgnoreCase(s2));
System.out.println("Compare String: "+s1.equalsIgnoreCase(s3));
}
}

Output

Compare String: true
Compare String: false

compareTo()

compareTo(): This method is used to compare two strings by taking unicode values, It return 0 if the string are same otherwise return +ve or -ve integer values.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy";
int i;
i=s1.compareTo(s2);
if(i==0)
{
System.out.println("Strings are same");
}
else
{
System.out.println("Strings are not same");
}
}
}

Output

Strings are not same

compareToIgnoreCase()

compareToIgnoreCase(): This method is case insensitive method, which is used to compare two strings similar to compareTo().

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="HITESH";
int i;
i=s1.compareToIgnoreCase(s2);
if(i==0)
{
System.out.println("Strings are same");
}
else
{
System.out.println("Strings are not same");
}
}
}

Output

Strings are same

startsWith()

startsWith(): This method return true if string is start with given another string, otherwise it returns false.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.startsWith("Java"));
}
}

Output

true

endsWith()

endsWith(): This method return true if string is end with given another string, otherwise it returns false.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.endsWith("language"));
}
}

Output

true

subString()

subString(): This method is used to get the part of given string.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.substring(8)); // 8 is starting index
}
}

Output

programming language

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.substring(8, 12));
}
}

Output

prog

indexOf()

indexOf(): This method is used find the index value of given string. It always gives starting index value of first occurrence of string.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.indexOf("programming"));
}
}

Output

8

lastIndexOf()

lastIndexOf(): This method used to return the starting index value of last occurence of the given string.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="Java is programming language";
String s2="Java is good programming language";
System.out.println(s1.lastIndexOf("programming"));
System.out.println(s2.lastIndexOf("programming"));
}
}

Output

8
13

trim()

trim(): This method remove space which are available before starting of string and after ending of string.

Example

class StringHandling
{
public static void main(String arg[])
{
String s="    Java is programming language    ";
System.out.println(s.trim());
}
}

Output

Java is programming language

split()

split(): This method is used to divide the given string into number of parts based on delimiter (special symbols like @ space , ).

Example

class StringHandling
{
public static void main(String arg[])
{
String s="contact@tutorial4us.com";
String[] s1=s.split("@");  // divide string based on @
for(String c:s1) //  foreach loop 
{
System.out.println(c); 
}
}
}

Output

contact
@tutorial4us.com

replace()

replace(): This method is used to return a duplicate string by replacing old character with new character.
Note: In this method data of original string will never be modify.

Example

class StringHandling
{
public static void main(String arg[])
{
String s1="java";
String s2=s1.replace('j', 'k');
System.out.println(s2);
}
}

Output

kava

No comments:

Post a Comment