[go: up one dir, main page]

0% found this document useful (0 votes)
34 views20 pages

Snew 8 Strings

This document discusses string processing in Java. It covers constructing strings, comparing strings, finding string length, extracting substrings, converting case and replacing/splitting strings. Methods like equals(), length(), charAt(), substring(), indexOf(), replace(), split() are explained with examples. Converting between strings, characters and numbers is also summarized.

Uploaded by

Sana Khattak
Copyright
© © All Rights Reserved
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)
34 views20 pages

Snew 8 Strings

This document discusses string processing in Java. It covers constructing strings, comparing strings, finding string length, extracting substrings, converting case and replacing/splitting strings. Methods like equals(), length(), charAt(), substring(), indexOf(), replace(), split() are explained with examples. Converting between strings, characters and numbers is also summarized.

Uploaded by

Sana Khattak
Copyright
© © All Rights Reserved
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/ 20

Object Oriented

Programming in Java

Strings

1
Motivations

Often you encounter the problems that


involve string processing and file input
and output.
Suppose you need to write a program to
replace all occurrences of a word with a
new word in a file. How do you solve this
problem?

2
The String Class
• Constructing a String:
• String message = "Welcome to Java“;
• String message = new String("Welcome to
Java“);
• String s = new String();
• Obtaining String length and Retrieving Individual Characters
in a string
• String Concatenation (concat)
• Substrings (substring(index), substring(start, end))
• Comparisons (equals, compareTo)
• String Conversions
• Finding a Character or a Substring in a String
• Conversions between Strings and Arrays
• Converting Characters and Numeric Values to Strings
3
Constructing Strings

String message = new String ("Welcome to Java");

Since strings are used frequently, Java provides a


shorthand initializer for creating a string:

String message = "Welcome to Java";

4
String Comparisons

5
String Comparisons
• equals

String s1 = new String("Welcome“);


String s2 = "welcome";

if (s1.equals(s2)){
System.out.println(“S1 and S2 have exact same contents”);
}

if (s1.equalsIgnoreCase(s2)) {
System.out.println(“S1 and S2 have the same contents”);
}
6
String Length, Characters, and
Combining Strings

8
Finding String Length

Finding string length using the length() method:

message = "Welcome";
message.length() (returns 7)

9
Retrieving Individual Characters in a String

• Do not use message[0]

• Use message.charAt(index)

• Index starts from 0

Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

message W e l c o m e t o J a v a

message.charAt(0) message.length() is 15 message.charAt(14)

10
Concatenation Operator

String firstName = “Amr”;


String lastName = “Al-Ibrahim”;
String fullName = lastName+” “+firstName;
 “Al-Ibrahim Amr”

• If one of the operands of the + operator is a string, the other is


automatically converted to string and the two strings are then
concatenated.
String course = “ICS”;
int code = 102;
String courseCode = course+code  “ICS102”

• We frequently use the concatenation operator in println statements.


System.out.println(“The area =“+area);
String Concatenation

String s3 = s1.concat(s2);

String s3 = s1 + s2;

12
Extracting Substrings

13
Substrings
• Example: String greeting = “Hello, World”;
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12

• Java provides two methods for this operation:


substring(start); Returns the substring from
start to the end of the string
substring(start, Returns a substring from
end); start to end but not including
the character at end.

Examples:
String sub = greeting.substring(0, 4);  “Hell”
String w = greeting.substring(7, 12);  “World”
String tail = greeting.substring(7);  “World!”
Extracting Substrings

You can extract a single character from a string using the


charAt method. You can also extract a substring from a
string using the substring method in the String class.

String s1 = "Welcome to Java";


String s2 = s1.substring(0, 11) + "HTML";

Indices 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

message W e l c o m e t o J a v a

message.substring(0, 11) message.substring(11)


15
Converting, Replacing, and Splitting Strings

java.lang.String
+toLowerCase(): String Returns a new string with all characters converted to lowercase.
+toUpperCase(): String Returns a new string with all characters converted to uppercase.
+trim(): String Returns a new string with blank characters trimmed on both sides.
+replace(oldChar: char, Returns a new string that replaces all matching character in this
newChar: char): String string with the new character.
+replaceFirst(oldString: String, Returns a new string that replaces the first matching substring in
newString: String): String this string with the new substring.
+replaceAll(oldString: String, Returns a new string that replace all matching substrings in this
newString: String): String string with the new substring.
+split(delimiter: String): Returns an array of strings consisting of the substrings split by the
String[] delimiter.

16
Examples
"Welcome".toLowerCase() returns a new string, welcome.
"Welcome".toUpperCase() returns a new string, WELCOME.
" Welcome ".trim() returns a new string, Welcome.
"Welcome".replace('e', 'A') returns a new string, WAlcomA.
"Welcome".replaceFirst("e", "AB") returns a new string,
WABlcome.
"Welcome".replace("e", "AB") returns a new string,
WABlcomAB.
"Welcome".replace("el", "AB") returns a new string,
WABlcome.

17
Finding a Character or a Substring in a String

java.lang.String
+indexOf(ch: char): int Returns the index of the first occurrence of ch in the string.
Returns -1 if not matched.
+indexOf(ch: char, fromIndex: Returns the index of the first occurrence of ch after fromIndex in
int): int the string. Returns -1 if not matched.
+indexOf(s: String): int Returns the index of the first occurrence of string s in this string.
Returns -1 if not matched.
+indexOf(s: String, fromIndex: Returns the index of the first occurrence of string s in this string
int): int after fromIndex. Returns -1 if not matched.
+lastIndexOf(ch: int): int Returns the index of the last occurrence of ch in the string.
Returns -1 if not matched.
+lastIndexOf(ch: int, Returns the index of the last occurrence of ch before fromIndex
fromIndex: int): int in this string. Returns -1 if not matched.
+lastIndexOf(s: String): int Returns the index of the last occurrence of string s. Returns -1 if
not matched.
+lastIndexOf(s: String, Returns the index of the last occurrence of string s before
fromIndex: int): int fromIndex. Returns -1 if not matched.

22
Finding a Character or a Substring in a
String

"Welcome to Java".indexOf('W') returns 0.


"Welcome to Java".indexOf('x') returns -1.
"Welcome to Java".indexOf('o', 5) returns 9.
"Welcome to Java".indexOf("come") returns 3.
"Welcome to Java".indexOf("Java", 5) returns 11.
"Welcome to Java".indexOf("java", 5) returns -1.
"Welcome to Java".lastIndexOf('a') returns 14.

23
Converting Strings to char

• char[] chars = "Java".toCharArray();

24
Convert Character and Numbers to Strings

The String class provides several static valueOf methods for converting a
character, an array of characters, and numeric values to strings. These
methods have the same name valueOf with different argument types char,
char[], double, long, int, and float.
For example, to convert a double value to a string, use
String.valueOf(5.44).
The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’.

25

You might also like