0% found this document useful (0 votes)
154 views18 pages

Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods

The document discusses the String class in Java, including how to create string objects, common string methods like length(), charAt(), substring(), indexOf(), and compareTo(), and how strings can be manipulated using methods like replace(), toUpperCase(), and trim(). Some of the main string operations covered are extracting characters and substrings, comparing strings, searching strings, and changing the case or replacing characters in strings.

Uploaded by

Neema
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views18 pages

Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods

The document discusses the String class in Java, including how to create string objects, common string methods like length(), charAt(), substring(), indexOf(), and compareTo(), and how strings can be manipulated using methods like replace(), toUpperCase(), and trim(). Some of the main string operations covered are extracting characters and substrings, comparing strings, searching strings, and changing the case or replacing characters in strings.

Uploaded by

Neema
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Strings

The objectives of this chapter are:

 To discuss the String class and some of its methods


1. String Basics
• A string is a sequence of characters
• The String class is defined in the “java.lang”
package
• Java has a predefined class called String
• Text in Java is stored as a String object
– Example: String s = “Java";
• The reference variable s refers to an object of the
String class

s “JAVA"
2. Creating String Objects

 Method 1 : with the new keyword.


String name = new String(“Java");

 Method 2: creating "implicitly":


String name;
name = “Java";

 Method 3: creating using the + operator. The + operator,


when applied to Strings means concatenation.
int age = 21;
String message = "Craig wishes he was " + age + " years old";
3. Common String Methods
• length() returns the number of characters in the string
– Example:
• String s=“Hello”
int l=s.length()
System.out.println(l);
5

• charAt(int position) returns the character at the given


position
• Example
– Char c=s.charAt(2);
System.out.println(l);
• l
3. Common String Methods
• substring(int n, int m) returns a new string
that is a substring of the original string,
• starting at the nth character and going up to
but not including the mth character
– Example:
String s = "Do you go to Western?";
String s1=s.substring(3, 6)
System.out.println(s1);
you
3. Common String Methods

• substring(int n) returns a new string which is a


substring of the original string, starting at the nth
character until the end of the string
– Example:

String s = "Do you go to Western?";


String s1=s.substring(7)
System.out.println(s1);
go to Western?
3. Common String Methods

• startsWith(String prefix) returns true if the string starts


with the prefix, otherwise returns false
– Example:
> String str1 = "Hello there";
> System.out.println(str1.startsWith("Hello"));
true
> System.out.println(str1.startsWith("Bye"));
false

• endsWith(String suffix) returns true if the string ends with


the suffix, otherwise returns false
System.out.println(str1.endsWith("Hello"));
false
> System.out.println(ends.startsWith("Bye"));
true
3. Common String Methods

• indexOf(String str) returns the starting


index of the parameter str if it is found. If it
isn’t found, returns -1
– Example:
> String str2 = "Hello there. Why are you there?";
> System.out.println(str2.indexOf("there"));
6

> System.out.println(str2.indexOf("their"));
-1
3. Common String Methods

• indexOf(String str, int fromIndex)


• returns the starting index of the parameter str, at or after
the parameter fromIndex.
• If it isn’t found, returns –1
– Example:
> String str1 = "Hello there";
Int i=str1.indexOf("there", 0);
> System.out.println(i);
6

Int i1=str1.indexOf("there", 7);


> System.out.println(i1);
-1
3. Common String Methods

• lastIndexOf() returns the last index of a


substring
– Example:
> String str2 = "Hello there. Why are you there?";
> System.out.println(str2.lastIndexOf("there"));
25
3. Common String Methods
• toUpperCase() returns a new string with all the characters in
uppercase
– Example:
– String s=“Hello”;
String s1=s. toUpperCase();
– > System.out.println(s1);
HELLO

• toLowerCase() returns a new string with all the characters in


lowercase
– Example:
– String s1=s. toLowerCase();
– > System.out.println(s1);
hello
3.Common String Methods
• compareTo(String str) returns an integer:
– negative if this String object is) “less than” than the parameter
string

– 0 if they are equal

– positive if this String object is “greater than” the parameter string

– Example:
> String str1 = "Bye";
> String str2 = "Hello";
> System.out.println(str1.compareTo(str2));
-6
> System.out.println(str2.compareTo(str1));
6
3. Common String Methods

– trim()
• Used to remove all the leading and trailing spaces
does not affect whites space in the middle

String word1 = “ Hi Bob “;


String word2 = word1.trim();
//word2 is “Hi Bob” – no spaces on either end
//word1 is still “ Hi Bob “ – with spaces
3. Common String Methods
Word1.replace(oldCh,newCh)
returns a new string formed from word1
by replacing all occurrences of oldCh with
newCh
String word1 = “rare“;
String word2 = “rare“.replace(‘r’, ‘d’);

//word2 is “dade”, but word1 is still “rare“


3. Common String Methods
equals(String str)
• We can also check whether two strings are the same
using the equals method of the String class

• Example: in the Interactions pane


> String test1 = "hello";
> String test2 = "Hello";
> System.out.println(test1.equals(test2));
false
> String test3 = "hello";
> System.out.println(test1.equals(test3));
true
Review Questions:

1. The String class is part of what


package?
2. What does the String class have that
other classes do not have?
3. “Text enclosed in quotes is called ?”
4. How do you declare an empty string?
5. “Bob” + “ “ + “Smith” is called ____ ?
Review (cont’d):
6. String city = "Bloomington“;
What is returned by city.charAt (2)?
7. By city.substring(2, 4)?
8. By city.lastIndexOf(‘o’)?
9. By city.indexOf(3)?
10. What does the trim method do?
Review (cont’d):
11. “sam”.equals(“Sam”) returns ?
12. What kind of value does
“sam”.compareTo(“Sam”) return?
13. What will be stored in s?
s = “mint”.replace(‘t’, ‘e’);
14. What does s.toUpperCase() do to s?

You might also like