Strings in Java: Sagar P
Strings in Java: Sagar P
SAGAR P
INTRODUCTION
• Strings are sequence of characters.
• In Java, Strings are objects.
• Strings are constant, their values cannot be changed
once they are created.Hence,they are immutable in
nature.
• Java provides String class to create & manipulate
strings.
• String Literal: A series of characters enclosed in
double quotes.
The Ways to Assign a String
• There are many ways to assign a String in Java
– String message;
– message = "Hello";
– String message = "Hello";
– String message;
– message = new String("Hello");
– String message = new String("Hello");
• All of these result in message being a reference variable
which points to the String object
new String(…) – a Constructor
– A constructor is a method of a class used to construct a
specific object
• The constructor is called whenever you instantiate an object using the
reserved word new
– The constructor performs the following tasks:
• Sets aside memory for the object
• Initializes elements of the object as specified by the constructor’s code
String Methods
• METHODS:
– length( )
• returns the length of the String (the number of characters stored in the String)
– charAt(i)
• returns the ith character of the String where the first character is counted as 0
– concat(x)
• returns a new String of this String concatenated with x afterward
– example: a.concat(b) returns the String a followed by the String b
– equals(x)
• returns true if this String and x are storing the exact same sequence of
characters, false otherwise
– equalsIgnoreCase(x)
• same as equals except that case is ignored
– toUpperCase( )
• returns the String where all letters are translated to their uppercase equivalent
– toLowerCase( )
• returns the String where all letters are translated to their lowercase equivalent
String Methods(contd…)
• compareTo(x)
– returns a negative number if this String < x, 0 if the two Strings are equal, a positive
number if this String > x
• Note that <, >, <=, >=, = = and != do not work for Strings, you must use equals,
equalsIgnoreCase and compareTo to compare Strings!
• compareToIgnoreCase(x)
– same as compareTo except case is ignored
• replace(ch1, ch2)
– returns a String with the same characters of this String except that all characters ch1 are
replaced by ch2 instead
• trim( )
– returns a String equivalent to this String except that all white space (blanks) at the
beginning and ending are removed
• endsWith(x)
– returns true if this String ends with the substring x, false otherwise
• startsWith(x), startsWith(x, f)
– same except sees if the String starts with the substring x and starts at position f in the
second case (recall that f is really the f+1 character since we start counting at 0)
indexOf Methods
• The indexOf method will find the position in the String of some
character or characters
• In other languages this operation is often called substring (find
the substring in the string)
– indexOf(c), indexOf(c, f)
• returns the int value of the first occurrence of character c in the String,
where the search starts at 0 or at f if f is included
• here the first character is considered to be location 0, so this return value is 1
less than the physical position if we start counting at 1, a –1 is returned if the
character c is not in the String
– indexOf(str, f)
• same except that str is a String in itself, f again is the starting point to begin
comparing
– substring(f)
• substring returns the String starting at position f through the end
String Concatenation
Strings are not like primitive objects and so manipulating a
String does not actually affect the given String but instead returns a new String