Thursday 10 November 2016

STRING



    In Java, strings are class objects and implemented using two classes, String and StringBuffer.
    A Java string is not a character array and is not NULL terminated.
      String may be declared and created as follows:
String stringname;
stringname=new String(“String”);
Ex:   String fname;
         fname=new String(“Santosh”);

         String fname=new String(“Santosh”);
        Java strings can be concatenated using the ‘+’ operator.
Ex:String name=name1+name2;
         String city=”New” +”York”                     

    String Arrays: String array can be created as follows:
Ex: String item[]= new String[3];
    String Methods: Some of the most commonly used methods on the string class are shown below:
Method Call
Task Performed
s2=s1.toLowerCase;
Converts the string s1 to all lowercase.
s2=s1.toUpperCase;
Converts the string s1 to all uppercase.
s2=s1.replace(‘x’,’y’);
Replace all appearances of x with y.
s2=s1.trim();
Remove white spaces at the beginning and end of the string s1.
s1.equals(s2);
Returns ‘true’ if s1 is equal to s2.
s1.equalsIgnoreCase(s2);
Returns ‘true’ if s1=s2, ignoring the case of characters.
s1.length();
Give the length of s1.
s1.CharAt(n);
Give nth character of s1.
s1.compareTo(s2);
Return negative if s1<s2, positive if s1>s2, and zero if s1=s2.
s1.concat(s2);
Concatenates s1 and s2.
s1.substring(n);
Give substring starting from nth character.
s1.substring(n,m);
Give substring starting from nth character up to mth.(not including mth)
String. Valueof (p);
Creates a string representation of the object p.
p.toString()
Creates a string representation of the object p.
s1.indexof(‘x’);
Give the position of the first occurrence of ‘x’ in the string s1.
s1.indexof(‘x’,n);
Give the position of ‘x’that occurs after nth position in the string s1.
String.valueof(Variable)
Converts the parameter value to string.

No comments:
Write comments