String Review of String 1 String is a

  • Slides: 20
Download presentation
String

String

Review of String (1) • String is a user-defined class that is created by

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

Review of String (2) • Defining String variables (object definition syntax): • The Java

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

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

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

Instance methods on String (1) • The general format to use an operation on

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

Instance methods on String (2) • length(): returns the length of the string •

Instance methods on String (2) • length(): returns the length of the string • char. At(n): returns the charcater at position n in the string

Instance methods on String (3) • substring(i, j): returns the substring from position i

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:

Comparison operations • The == operator in Java always compare whether the value stored

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

Comparing if strings are equal based on their content (1) • The instance method

Comparing if strings are equal based on their content (1) • The instance method used to compare equality of two strings s 1 and s 2 based on their content:

Comparing if strings are equal based on their content (2) • The instance method

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

Ranking 2 strings (1) • The instance method used to rank of two strings

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

Ranking 2 strings (2) • The instance method used to rank of two strings

Ranking 2 strings (2) • The instance method used to rank of two strings s 1 and s 2 without considering upper/lower case in alphanumerical order:

Finding substrings (1) • Finding (locate) the first occurrence of a substring sub in

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

Finding substrings (2) • The instance method used to find (locate) the last occurrence

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

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

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

Transformation Operations (2) • Replace the first occurrence of a substring old inside a

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

Strings in Java are immutable • The instance methods in the String class do

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

String and Array (1) • a String is a single object • an array

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:

String and Array (2) • convert an array of char into a String object,

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

String and Array (3) • The chances are very good that the Java library

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