Strings in Java
Strings in Java
Prepared By
Dr.S.Visnu Dharsini
Assistant Professor
CSE
• String Handling – String Constructors, String Length, Special
String Operations -Character Extraction, String Comparison,
Searching Strings, Modifying Strings, using valueOf(),Comparison
of String.
1 ) String Literal
Java String literal is created by using double quotes.
For Example: String s="welcome";
2) By new keyword
String s=new String("Welcome");
• In java, string is basically an object that represents
sequence of char values. Java String provides a
lot of concepts that can be performed on a string
such as compare, concat, equals, split, length,
replace, compareTo, intern, substring etc.
• In java, string objects are immutable.
Immutable simply means unmodifiable or
unchangeable.
String s="javatpoint";
• There are two ways to create String object:
1. By string literal
2. By new keyword
1 ) String Literal
Java String literal is created by using double
quotes.
For Example: String s="welcome";
2) By new keyword
String s=new String("Welcome");
STRING CONSTRUCTORS
8. String(byte bytes[], int offset, int length, String charsetName): Similar to above
except that the character set encoding name is passed as a string. This will throw
UnsupportedEncodingException if the encoding is not supported.
9. String(char value[]): creates the string object from the character array.
10. String(char value[], int offset, int count): The offset specifies the index of the first
character. The length specifies the number of characters to use. This constructor
throws IndexOutOfBoundsException if offset is negative, length is negative, or
offset is greater than value.length - length.
11. String(int[] codePoints, int offset, int count): creates a string from the input Unicode
code points array. It throws IllegalArgumentException if any of the code points are
invalid. This constructor throws IndexOutOfBoundsException if the offset is
negative, the length is negative, or offset is greater than codePoints.length - length.
12. String(StringBuffer buffer): creates a new string from the contents of the string
STRING CONSTRUCTORS-
CONTD
String(byte[] byte_arr)
Construct a new String by decoding the byte array. It uses the
platform’s default character set for decoding.
Example
Example: Example:
The getChars method copies characters from a string into the destination
character array.
THANK YOU