0% found this document useful (0 votes)
53 views

Strings: Create String

Strings in Java are objects of the String class that are immutable. A String can be created using new String(), as a literal, or from a character array. The String class has many methods like length(), charAt(), toLowerCase(), and trim() to work with and manipulate string values.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Strings: Create String

Strings in Java are objects of the String class that are immutable. A String can be created using new String(), as a literal, or from a character array. The String class has many methods like length(), charAt(), toLowerCase(), and trim() to work with and manipulate string values.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Strings

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

1. String obj=new String("string");

Ex : String name=new String("Raju");

2 . String item="cinthol";

3. Use character array

char name[10]={'a','s','h','o','k'};

String name1=new String(name);

String class Methods


METHOD DESCRIPTION
length() returns length of string
charAt(index) return character at specified index
indexOf('char') returns index of specified character
which is first occurrence
lastIndexOf('char') returns last index of specified
character which is first
occurance
toLowerCase() returns string into lower case
toUpperCase() returns string into upper case
trim() remove spaces from begin and end of
string
replace('old','new') replaces specified old character with
new character
concat(string) concatenate strings
equals(string) to compare string content and
returns true if string equals or
otherwise returns false.
equalsIgnoreCase(string) To compare string content without
case-sensitive and returns true if
string equals otherwise returns false.
comapreTo(string) To compare one string with another
string.
split() decompose the entire string into
smaller pieces where space or
required delimiter occurred.

Example on length, charAt(), toLowerCase(), toUpperCase()

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());
}
}

You might also like