Presentation is loading. Please wait.

Presentation is loading. Please wait.

String.

Similar presentations


Presentation on theme: "String."— Presentation transcript:

1 String

2 Review of String (1) String is a user-defined class that is created by the programmers who implemented the Java library

3 Review of String (2)  Defining String variables (object definition syntax): The Java compiler recognize a short hand notation for String creation:

4 How a String object is stored
A String is an object:

5 Instance methods on String (1)
The general format to use an operation on a String object:

6 Instance methods on String (2)
length(): returns the length of the string charAt(n): returns the charcater at position n in the string

7 Instance methods on String (3)
substring(i,j): returns the substring from position i up to (not including) position j of the string Concatenating strings:

8 Comparison operations
The == operator in Java always compare whether the value  stored in variables are equal. The test x == y does not test whether the strings stored in x and y are equal to one another

9 Comparing if strings are equal based on their content (1)
The instance method used to compare equality of two strings s1 and s2 based on their content:

10 Comparing if strings are equal based on their content (2)
The instance method used to compare equality of two strings s1 and s2 without considering upper/lower case difference:

11 Ranking 2 strings (1) The instance method used to rank of two strings s1 and s2  in alpha-numerical order:

12 Ranking 2 strings (2) The instance method used to rank of two strings s1 and s2  without considering upper/lower case in alpha- numerical order:

13 Finding substrings (1) Finding (locate) the first occurrence of a substring sub in a string s:

14 Finding substrings (2) The instance method used to find (locate) the last occurrence of a substring sub in a string s is:

15 Transformation Operations (1)
Transform all upper case letters in a string s into lower case letters Transform all lower case letters in a string s into upper case letters

16 Transformation Operations (2)
Replace the first occurrence of a substring old inside a string s by the substring new replace all occurrences of a substring old inside a string s by the substring new

17 Strings in Java are immutable
The instance methods in the String class do not change  the implicit parameter (the string)

18 String and Array (1) a String is a single object
an array of char are a series (multiple) of variables Converts a string into an array of char:

19 String and Array (2) convert an array of char into a String object, we use a constructor method in the String class:

20 String and Array (3) The chances are very good that the Java library contains  instance methods that help you write your program


Download ppt "String."

Similar presentations


Ads by Google