JAVATECH NEWS

Java

CORE JAVA

Interview

OFF-CAMPUS

Saturday, 15 November 2014

Example of String class methods.



Example of String class methods are-

Here we will explain all the String Class methods with example,in this post you will be able to learn the various String class method with their examples,examples are explained in easy manner to understand easily,String class methods consist 14 methods like length(),charAt(int i),compareTo(String s),compareToIgnoreCase(String s),boolean equals(String s),boolean equalsIgnoreCase(String s),int indexOf(String s),String replace(char c1,char c2),String substring(int i),String substring(int i1,int i2),String toLowerCase(),String toUpperCase(),String trim() etc in this post.Now lets see all the methods one by one.

1.int length():This method is used to find the length or number of character in a string,
Eg:

class Example
{
public static void main(String args[])
{
String s1="My name is Rahim";
System.out.println("Length of String is :"+s1.length());
}
}
O/P:
Length of String is :16

2.char charAt(int i):This method is used to get the character at specific location.

class StringExample
{
public static void main(String args[])
{
String s="i am Rahim";
char ch=s.charAt(6);
System.out.print(ch);
}
}
O/P:
a

3.int compareTo(String s):This method is used to compare two strings and find which string is bigger and smaller,its return type is int.If  both string are equal then it returns 0(zero),If the argument string is greater than this string it returns positive integer, If the argument string is smaller  than this string it returns negative integer.Here is an example:

public class StringComparision
{
       
 
      public static void main(String[] args)
      {
   
      String s1="My name is Raju";
      String s2="My name is Raju";
      String s3="I am from Mumbai India";
            int  x=s1.compareTo(s2);
            System.out.println(x);

            int y=s2.compareTo(s3);
            System.out.println(y);

           int z=s3.compareTo(s2);
            System.out.print(z);
         
    }

}
O/P:
0
4
-4

4.int compareToIgnoreCase(String s): This method is same as previous method but it does not take the case of string in consideration.


5.boolean equals(String s): This method is used to find the strings are same or different by considering the case of string, it  returns true if strings are same, otherwise false.Eg:

public class StringComparision1
{
       
 
      public static void main(String[] args)
      {
   
      String s1="My name is Raju";
      String s2="My name is Raju";
      String s3="MY NAME IS RAJU";
            boolean  x=s1.equals(s2);
            System.out.println(x);

            boolean y=s2.equals(s3);
            System.out.println(y);

             }

}
O/P:
true
false


 7. boolean equalsIgnoreCase(String s):Same as previous method but it does not take case of strings into consideration.


8.int indexOf(String s): it will return the value of substring in main string, if substring is not found then it will return some negative value. .Eg:


class IndexOf
{
public static void main(String args[])
{
String s1="I like Java Programming";
String s2="Java";
int x=s1.indexOf(s2);
System.out.print(x);
}
}
O/P:
7

9.String replace(char c1,char c2) :it replaces the characters c1 with new character c2 in String
Eg:


class Replace
{
public static void main(String args[])
{
String s1="Malayalam";
String x=s1.replace('a','e');
System.out.print(x);
}
}
O/P:
Meleyelem

10.String substring(int i):this method extracts sub string from main string.
Ex:


class SubString
{
public static void main(String args[])
{
String s="I am from India";
String result=s.substring(5);
System.out.println(result);
}
}
O/P:
from India

11.String substring(int i1,int i2): returns a new string consisting of all character starting from i1 to i2(excluding character i2).

Ex:

class SubString
{
public static void main(String args[])
{
String s="I am from India";
String result=s.substring(5,9);
System.out.println(result);
}
}
O/P:
from

12.String toLowerCase(): returns the string after converting all character to lowercase.

Ex:
class ToLowerCase
{
public static void main(String args[])
{
String s="I LIKE JAVA";
String result=s.toLowerCase();
System.out.print(result);
}
}
O/P:
i like java

13.String toUpperCase():returns the string after converting all character to uppercase.

Ex:
class ToUpperCase
{
public static void main(String args[])
{
String s="i like java";
String result=s.toUpperCase();
System.out.print(result);
}
}
O/P:
I LIKE JAVA

14.String trim(): to remove the unnecessary space from the beginning and end of the string.

class Trim
{
public static void main(String args[])
{
String s=" Hi ";
String result=s.trim();
System.out.print(result);
}
}
O/P:
Hi

No comments:

Post a Comment

Designed By Blogger Templates