iTSCET

iT Portal

String class Important methods

Java.lang-

String class Important methods

Using the java.lang.String Class

Strings Are Immutable Objects

concat(); method ‘appends a literal to the end

eg: String s = “abcdef”;
s = s.concat(” more stuff”);

The overloaded + and += operators perform functions similar to the concat() Method.

eg:
String s1 = “spring “;
String s2 = s1 + “summer “;
s1 += “winter “;
System.out.println(s1 + ” ” + s2); // o/p: spring winter spring summe

toUpperCase();

eg: s=s.toUpperCase();

toLowerCase();

eg: s=s.toLowerCase();

charAt();

eg: String x = “airplane”;
System.out.println( x.charAt(2) );

equalsIgnoreCase();

eg: String x = “Exit”;
System.out.println( x.equalsIgnoreCase(“EXIT”));

length()

eg: String x = “01234567”;
System.out.println( x.length() ); // returns “8”

replace(char old, char new)

eg: String x = “oxoxoxox”;
System.out.println( x.replace(‘x’, ‘X’) );

substring(int begin)
substring(int begin, int end)

eg:
String x = “0123456789”; // as if by magic, the value of each char
// is the same as its index!
System.out.println( x.substring(5) ); // output is “56789”
System.out.println( x.substring(5, 8)); // output is “567”

trim()

eg:
String x = ” hi “;
System.out.println( x + “x” ); // result is ” hi x”
System.out.println( x.trim() + “x”); // result is “hix”

Example program covering all String Methods :

public class StringClassMethods
{
public static void main(String args[])
{
String s = “KfC”;
// trim()
String x = ” Are you hungry “;
System.out.println( x + “?” );
System.out.println( x.trim()+ “?”);
// public String substring(int begin)
// public String substring(int begin, int end)
System.out.println( s.substring(1) );
System.out.println( s.substring(0, 3));
// replace(char old, char new)
System.out.println( s.replace(‘f’, ‘F’) );
System.out.println( s );
// length()
System.out.println( s.length() );
// public boolean equalsIgnoreCase(String s)
System.out.println( s.equalsIgnoreCase(“kFC”));
System.out.println( s.equalsIgnoreCase(“afc”));
// concat()
s = s.concat(” more chicken”);
System.out.println( s.concat(” more stuff”) );
s += ” In fact more price”;
System.out.println( s );
// toUpperCase()
s=s.toUpperCase();
System.out.println( s );
// toLowerCase()
System.out.println( s.toLowerCase());
// charAt()
System.out.println( s.charAt(2) );
System.out.println( s.equalsIgnoreCase(“kfc more chicken in fact more price”));

}
}

Output:

Are you hungry ?
Are you hungry?
fC
KfC
KFC
KfC
3
true
false
KfC more chicken more stuff
KfC more chicken In fact more price
KFC MORE CHICKEN IN FACT MORE PRICE
kfc more chicken in fact more price
C
true

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s