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

String

Uploaded by

s.hemaswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

String

Uploaded by

s.hemaswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

String

String
Implementing strings as built-in objects
allows Java to provide a full complement of
features that make string handling convenient.

The String Constructors


String s = new String();
String(char chars[ ], int startIndex, int numChars)
char chars[] = { 'a', 'b', 'c' }; String s = new
String(chars)
Program : 1
..\java-the-complete-reference-8th-edition.pdf
String Length

The length of a string is the number of


characters that it contains. To obtain this value,
call the length( ) method, shown here:
int length( )
Program2:
String Concatenation
Java does not allow operators to be applied to
String objects.
The one exception to this rule is the + operator,
which concatenates two strings, producing a
String object as the result.
String age = "9";
String s = "He is " + age + " years old.";
System.out.println(s);
charAt( )
• To extract a single character from a String, you can refer
directly to an individual character via the charAt( ) method.
• char charAt(int where)
• The value of where must be nonnegative and specify a
location within the string.
• charAt( ) returns the character at the specified location.
• For example,
char ch;
ch = "abc".charAt(1);
assigns the value b to ch.
String Comparison
• equals( ) and equalsIgnoreCase()

boolean equals(Object str)


boolean equalsIgnoreCase(String str)

Program3:
startsWith( ) and endsWith( )
boolean startsWith(String str)
boolean endsWith(String str)

For example,
"Foobar".endsWith("bar")
"Foobar".startsWith("Foo")
are both true.

"Foobar".startsWith("bar", 3)  TRUE
compareTo( )
• int compareTo(String str)
if(s1.compareTo(s2))
Searching Strings
• indexOf( ) Searches for the first occurrence of a
character or substring.
• lastIndexOf( ) Searches for the last occurrence of
a character or substring
int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)
Program 4
• substring( )
• The first is String substring(int startIndex)
• The second form of substring( ) allows you to
specify both the beginning and ending index
of the substring:
• String substring(int startIndex, int endIndex)
• Program 5:
concat( )
• String concat(String str)
• String s1 = "one";
• String s2 = s1.concat("two");
trim( )
• Method returns a copy of the invoking string
from which any leading and trailing
whitespace has been removed. It has this
general form: String trim( )
• Here is an example:
• String s = " Hello World ".trim();

You might also like