Configure HTML/JavaScript

Tuesday, September 25, 2012

Java String class, Constructs and methods

Java String Class:

Constructs
Java string class have many constructs to handle java strings. this constructs takes different parameters to handle java string in different manners.

String s = new String(); //string empty constructs

char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());  //out put will abc

Methods:

it also provides many methods to handle strings. one of the simplest method is length method. it will return the length of the string.

String s = new String(chars);
System.out.println(s.length());

Other methods are their to handle string. please refer complete reference java for the complete details.

StringBuffer class:

it provides many methods to handle advanced string. like you can append string at a special position. you can insert string, etc



// Demonstrate append().
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append("a = ").append(a).append("!").toString();
System.out.println(s);
}
}
The output of this example is shown here:
a = 42!



No comments:

Post a Comment