String Handling: - in Java
String Handling: - in Java
• in Java
– string is a sequence of characters
• But not character arrays rather object of String.
• Fixed and immutable.
– a new String object is created for modified strings.
• StringBuffer
– For modifiable strings
• Both String & StringBuffer are final.
• Create
• Search
• Modify
• Compare 2-CSE-'C'sec sastra 2017-2018
• String creation
– String objects that are created using literal are stored in a special memory
area known as string constant pool.
– literal
• String s1 = “java”;
• String s2 = “java”;
• Only one object is created. While accessing s2, the reference s1 will be returned.
– new keyword
• String()
– String s = new String();
• String(char chars[ ])
– char chars[] = { 'a', 'b', 'c' };
– String s = new String(chars);
• String(String strObj)
– String s1 = new String(s);
• String(char chars[ ], int startIndex, int numChars)
– String s = new String (chars, 1, 2);
• String(byte asciiChars[ ])
– byte ascii[] = {65, 66, 67, 68, 69, 70 };
– String s= new String(ascii);
• String(byte asciiChars[ ], int startIndex, int numChars)
– String s1 = new String(ascii,2,5);
4 static String format(Locale l, String format, Object... returns formatted string with given
args) locale
6 String substring(int beginIndex, int endIndex) returns substring for given begin
index and end index
13 String replace(char old, char new) replaces all occurrences of specified char value
17 String split(String regex, int limit) returns splitted string matching regex and limit
20 int indexOf(int ch, int fromIndex) returns specified char value index starting with
given
2-CSE-'C'sec sastra index
2017-2018
21 int indexOf(String substring) returns specified substring index
• StringBuffer reverse( )
• StringBuffer delete(int startIndex, int endIndex)
– End index is not included
• StringBuffer deleteCharAt(int loc)
2-CSE-'C'sec sastra 2017-2018
• StringBuffer replace(int startIndex, int endIndex,
String str)
– Length of str > endindex – start index
• Remaining values will be appended into stringbuffer and increase
the size of the string
– Length of string < end index – start index
• The characters in the exceeding positions will be deleted.