Strings

Strings
String ConstructorsThe String class supports several constructors. To create an empty String, you call the default constructor. For example,

1)  String s = new String();
2)  Char chars[ ] = { ‘a’, ‘b’, ‘c’ };
String s = new String(chars);
3)  String(char chars[ ], int startIndex, int numChars)
Here, startIndex specifies the index at which the subrange begins, and numChars
specifies the number of characters to use. Here is an example:
4)  Char chars[ ] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ };
String s = new String(chars, 2, 3);
5)  String(String strObj)

String Methods

Method call Task performed
s2 = s1.toLowerCase; Converts the string to all lowercase
s2 = s1.toUpperCase; Converts the string to all uppercase
s2 = s1.replace(‘ x ‘ , ‘ y ‘ ); Replace all appearance of x with y
s2 = s1.trim(); Remove white spaces at the beg 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 () Gives the length of s1
s1.CharAt ( n ) Gives nth character of s1
s1.CompareTo( s2 ) Return – ve if s1 < s2, + ve if s1 > s2 and zero if s1 = s2
s1.concat ( s2)Concatenates s1 and s2
s1.substring( n ) Gives substring starting from nth character
s1.substring( n , m ) Gives substring start from nth character up to mth(exclud mth)
String.ValueOf ( p ) Creates a string object of the parameter p(simple or object)
p.toString () Creates a string representation of the object p
s1.indexOf( ‘ x ‘ ) Gives the position of the first occurrence of ‘x’ in the string s1
s1.indexOf( ‘ x ‘ , n) Gives the pos of ‘ x ‘ that occurs after nth position in string s1
String.ValueOf ( var) Converts the parameter value to string representation
StringBuffer

StringBuffer represents growable and writeable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end. StringBuffer will automatically grow to make room for such additions and often has more characters preallocated than are actually needed.

StringBuffer Constructors
StringBuffer defines these three constructors:

  • StringBuffer( )
  • StringBuffer(int size)
  • StringBuffer(String str)
StringBuffer Methods

Method call Task performed
s1.setCharAt ( ' n ' , ' x ' ) Modified the nth character to x
s1.append (s2)Appends the string s2 to s1 at the end
s1.insert ( n , s2 ) Inserts the string s2 at the nth position of string s1
s1.setLength ( n ) Sets the length of the string s1 to n
s1.getChars(sourceStart,sourceEnd, target[ ],targetStart)Gives reversed characters in stringBuffer object
s1.reverse(); Gives reversed characters in stringBuffer object
s1.replace(startIndex,endIndex,str)The substring being replaced is specified by the indexes startIndex and endIndex.
s1.delete(startIndex, endIndex)The substring being replaced is specified by the indexes startIndex and endIndex.
s1.substring(startIndex,endIndex)The substring being replaced is specified by the indexes startIndex and endIndex.




Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.