String Handling Module 3
String Handling Module 3
Module 3
Contents
• The String Constructors,
• Special String Operations,
• Character Extraction,
• String Comparison,
• Searching Strings,
• Modifying a String,
• Data Conversion Using valueOf(),
• String Buffer,
• String Builder
What is String ?
• Strings, which are widely used in Java programming, are a sequence of
characters.
• In Java programming language, strings are treated as objects.
• The java.lang.String class is used to create string object.
• The Java platform provides the String class to create and manipulate
strings.
String – Object or Primitive?
• Strings could be considered a primitive type in Java, but in fact they
are not.
• A String is actually made up of an array of char primitives.
What is String ?
String objects are immutable!
• That means once a string object is created it cannot be altered.
• For mutable string, you can use StringBuffer and StringBuilder
classes.
2. By new keyword
For Example:
String s=new String("Welcome");
Java String Example
Output :
java
strings
example
The String Constructors
The String Constructors
• The String class supports several constructors. To create an empty String, call
the default constructor. For example,
String s = new String();
• will create an instance of String with no characters in it.
• The String class provides a variety of constructors to handle this. To create a
String initialized by an array of characters, use the constructor shown here:
String(char chars[ ])
• Here is an example:
The String Constructors
• You can specify a subrange of a character array as an initializer
using the following constructor:
String(char chars[ ], int startIndex, int numChars)
• Here, startIndex specifies the index at which the subrange begins,
and
• numChars specifies the number of characters to use.
• Here is an example:
The String Constructors
• You can construct a String object that contains the same character sequence as another
String object using this constructor:
String(String strObj)
• Here, strObj is a String object.
• Consider this example:
The String Constructors
• Even though Java’s char type uses 16 bits to represent the basic Unicode
character set, the typical format for strings on the Internet uses arrays of 8-bit
bytes constructed from the ASCII character set.
• Because 8-bit ASCII strings are common, the String class provides
constructors that initialize a string when given a byte array. Two forms are
shown here:
String(byte chrs[ ])
String(byte chrs[ ], int startIndex, int numChars)
• In each of these constructors, the byte-to-character conversion is done by using
the default character encoding of the platform.
The String Constructors
• The following program illustrates these the byte-to-character conversion is
done by using the default character encoding of the platform for each
constructors :
Using Using +
concat() operator
method
Concatenates
two or more
string.
String Concatenation with Other Data Types
• You can concatenate strings with other types of data. For example,
• In this case, age is an int rather than another String, but the output produced is
"He is 9 years old.". This is because the int value in age is automatically
converted into its string representation within a String object.
• Be careful when you mix other types of operations with string concatenation
expressions, however. You might get surprising results. Consider the following:
• Here, sourceStart specifies the index of the beginning of the substring, and
sourceEnd specifies an index that is one past the end of the desired substring.
getBytes( )
• There is an alternative to getChars( ) that stores the characters in
an array of bytes.
• This method is called getBytes( ), and it uses the default
character-to-byte conversions provided by the platform. Here is its
simplest form:
byte[ ] getBytes( )
• Other forms of getBytes( ) are also available. getBytes( ) is most
useful when you are exporting a String value into an environment
that does not support 16-bit Unicode characters.
toCharArray( )
• If you want to convert all the characters in a String object into a
character array, the easiest way is to call toCharArray( ).
• It returns an array of characters for the entire string. It has this
general form:
char[ ] toCharArray( )
• This function is provided as a convenience, since it is possible to
use getChars( ) to achieve the same result.
String Comparison
String Comparison
• String comparison can be done in 6 ways.
Using
By CompareTo()
equalsIgnoreCase( )
method
method
Using == operator By
regionMatches( )
method
"Foobar".endsWith("bar")
"Foobar".startsWith("Foo")
• are both true.
• A second form of startsWith( ), shown here, lets you specify a starting point:
"Foobar".startsWith("bar", 3)
• returns true.
equals( ) Versus ==
• It is important to understand that the equals( ) method and the == operator perform two
different operations.
• the equals( ) method compares the characters inside a String object.
• The == operator compares two object references to see whether they refer to the same
instance.
compareTo( )
• A string is less than another if it comes before the other in dictionary order. A
string is greater than another if it comes after the other in dictionary order.
• The method compareTo( ) serves this purpose.
• It is specified by the Comparable<T> interface, which String implements. It
has this general form:
int compareTo(String str)
• Here, str is the String being compared with the invoking String. The result of
the comparison is returned and is interpreted as shown here:
compareTo( )
• Here is a sample program that sorts an array of strings. The program uses
compareTo( ) to determine sort ordering for a bubble sort: