Strings: Create String
Strings: Create String
A collection characters
in java a string is an object of class "String"
In Java the String objects are not modifiable i.e the content
of string not change so we called they are immutable
objects.
Create string
The following are ways to create a string object
2 . String item="cinthol";
char name[10]={'a','s','h','o','k'};
class demo1
{
public static void main(String args[])
{
String str1="This is book on java, I like it";
int len=str1.length();
System.out.println(str1);
System.out.println("The length is "+len);
System.out.println("The character at 8th position is
"+str1.charAt(8));
System.out.println("The lowercase string is
"+str1.toLowerCase());
System.out.println("The Upper case string is
"+str1.toUpperCase());
System.out.println(str1);
}
}
Example on trim(), indexOf(), lastIndexOf()
class demo2
{
public static void main(String args[])
{
String str1=" This is book on java, I like it ";
System.out.println("The index of 'j' is "+str1.indexOf('is'));
System.out.println("The last index of 's' is "+str1.indexOf('is'));
System.out.println(str1);
System.out.println("With trim");
System.out.prinln(str1.trim());
}
}