Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

STRINGS IN JAVA

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

If we have a String x = "Hello" and a String y = " there "


Then x.concat(y) does not add there to Hello, but returns a new
String as shown below:
Character Methods
• Like Integer.parseInt and Double.parseDouble, we have
similar methods for the Character class
– Character.isDigit(ch)
• returns true if ch is a character that stores a digit ‘0’ – ‘9’, false otherwise
– Character.isLetter(ch)
• true if ch is a letter, false otherwise
– Character.isLowerCase(ch), Character.isUpperCase(ch)
• true if ch is a letter and in lower case/upper case, false otherwise
– Character.isWhiteSpace(ch)
• white space is a blank
– Character.toLowerCase(ch), Character.toUpperCase(ch)
• returns the same character but in lower/upper case form if ch is a letter
– Character.toString(ch)
• returns ch as a String, not a char
Common Errors
• Using = = and != to compare Strings
• Forgetting to properly concatenate Strings
– example:
• System.out.println("Information is not knowledge, knowledge is not wisdom");
• This yields an error because this is two separate Strings (on two separate lines)
but the two Strings are not properly concatenated together
• To fix this, we would use System.out.println("Information is not knowledge,
knowledge " + "is not wisdom");
• Forgetting that the first character of a String is at position 0 for
operations like charAt and indexOf
• Forgetting that String methods do not alter the String but instead
return a new String
THANK YOU

You might also like