3 4 String
3 4 String
0
3.0 4.0
Exception handling
Introduction
A string is a sequence of characters.
String Declaration
Example 1 char str [ ] = {'a','e','i','o','u'};
String Declaration
Example 2 char text[ ] = {'I','','C','A','N','','W','R','I','T', 'E','A','','G','O','O','D','','P','R','O','G','R','A','M','I','N','',' J','A','V','A'}; Example 3 String text = "I CAN WRITE A GOOD PROGRAM IN JAVA";
java.lang Package
class String
class StringBuffer
method
method
String class is present in java.lang package. The methods in this class are used to manipulate strings. The string objects created in a String class cannot be changed. Example:
String obj = new String("Welcome"); obj.append("_World"); System.out.println(obj);
wrong
String Methods
The String class has several methods that can be used to manipulate the string values. Each method has its own characteristic.
length() method
Is used to find the number of characters in a string.
length() method
Example 1 String s = "I am going to school"; int numchar = s.length( ); System.out.println(numchar); Output 20
length() method
Example 2 String s = " "; int numchar = s.length( ); System.out.println(numchar); Output 2
length() method
Syntax int variable_name = stringobject.length( );
concat() method
Example 1 String firststring = "The Twin Tower"; String secondstring = " looks beautiful"; String thirdstring = firststring.concat(secondstring); @ firststring+secondstring Output The Twin Tower looks beautiful
concat() method
Syntax String Stringvariable3 = stringvariable1.concat (stringvariable2);
concat() method
Example 2 String s1 = "Result: "+5+1; System.out.println(s1); Output Result: 51
concat() method
Example 3 String s2 = "Result: "+(5+1); System.out.println(s2); Output Result: 6
charAt() method
Extracts a single character from a string
charAt() method
Example String s = "Malaysia" char ch = s.charAt(4); System.out.println(ch); Output y
charAt() method
Syntax char variablename = stringobject.charAt(int where);
equals() method
Compares two strings for equality.
equals() method
Example String s1="Both are equal"; String s2="Both are equal"; String s3="Both are not equal"; System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3));
Output true false
equals() method
Syntax boolean variablename = stringobject.equals(stringobject);
indexOf() method
Will search the first occurrence of a character or set of characters inside a string.
indexOf() method
indexOf() method
Example 2 int val2 = s.indexOf("is"); System.out.println(val2); Output 2
indexOf() method
Syntax int variablename = stringobject.indexOf(character); int variablename = stringobject.indexOf(string);
lastIndexOf() method
Will search the last occurrence of a character or a set of characters inside a string.
lastIndexOf() method
lastIndexOf() method
Example 2 int val2 = s.lastIndexOf("is"); System.out.println(val1); Output 5
lastIndexOf() method
Syntax int variablename = stringobject.lastIndexOf(character); int variablename = stringobject.lastIndexOf(string);
replace() method
Will replace all occurrences of a character in a string with another character.
replace() method
Example String s="Hewwo"; String s1 = s.replace('w','l'); System.out.println(Old string= + s); System.out.println(New string= + s1); Output Hewwo Hello
replace() method
Syntax String stringvariable = stringobject.replace(original,replacement);
trim() method
Will trim the leading and trailing white spaces in a string.
trim() method
Example String s=" Hello Malaysia "; String s1 = s.trim( ); System.out.println(s1); Output Hello Malaysia
trim() method
Syntax String stringvariable = stringobject.trim( );
toLowerCase() method
Converts all the characters in a string from uppercase to lowercase.
toLowerCase() method
Example String s1="LOWER"; String s2 = s1.toLowerCase( ); System.out.println(s2); Output lower
toLowerCase() method
Syntax String stringvariable = stringobject.toLowerCase( );
toUpperCase() method
Converts all the characters in a string from lowercase to uppercase.
toUpperCase() method
Example String s1="upper"; String s2 = s1.toUpperCase( ); System.out.println(s2); Output UPPER
toUpperCase() method
Syntax String stringvariable = stringobject.toUpperCase( );
substring() method
Extracts a part of the string.
substring() method
Example String s1="I do shopping in Mega Mall"; String s2 = s1.substring(5,12); System.out.println(s2);
Akrasa ke (5) sehingga aksara ke(12-1)
Output shoppin
Is a special class supported by Java to handle strings. StringBuffer creates strings of flexible length whereas String class creates strings of fixed length.
length() method
Finds the current length of a StringBuffer object.
length() method
Example StringBuffer sb1 = new StringBuffer(Java); System.out.println (sb1.length()); Output 4
capacity() method
Returns the amount of memory allocated for the StringBuffer object.
capacity() method
Example
StringBuffer sb = new StringBuffer("Capacity of sb"); System.out.println (sb.capacity());
* Capacity of sb = 14 + 16(free storage) = 30 (amount of storage)
Output 30
capacity() method
Example
StringBuffer sb1 = new StringBuffer(""); System.out.println (sb1.capacity());
* Capacity of sb = 16(free storage) = 16 (amount of storage)
Output 16
append() method
Allows data to be added to the end of a StringBuffer object.
append() method
Example StringBuffer sb1 = new StringBuffer("Welcome to beautiful"); sb1.append("_World"); System.out.println(sb1);
setCharAt() method
Sets the character at any position within a StringBuffer.
setCharAt() method
Example StringBuffer sb1 = new StringBuffer("Hello World"); sb1.setCharAt(5,'_'); System.out.println(sb1);
Output Hello_World
insert() method
Allows data to be added at a specified position in the StringBuffer object.
insert() method
Example StringBuffer sb1 = new StringBuffer("Hello World"); sb1.insert(6,"Java "); System.out.println(sb1);
reverse() method
Example StringBuffer sb=new StringBuffer("roweT"); sb.reverse( ); System.out.println(sb);
Output Tower
delete() method
Example StringBuffer sb=new StringBuffer(Program"); sb.delete(2, 5 ); //(2,(5-1)) System.out.print(sb);
Output Pram
deleteCharAt() method
Example StringBuffer sb=new StringBuffer(Java"); sb.deleteCharAt(1 ); System.out.print(sb);
Output Jva