n n In Java strings are objects that
n n In Java, strings are objects that belong to class java. lang. String class objects work with complete strings. n n Instead of treating them as character arrays as some languages do Strings are immutable. n Whenever it looks as if a String object was modified, a new String was actually created and the old one was thrown away.
n n As it is a common class, Java permits you to skip the package name, even without a corresponding import statement. For example, String username; is equivalent to java. lang. String username;
n public String() : A new empty string; n String (string astring) n n Constructs a new string String greeting = "Hello world!"; String str=new String(“Hello world!“) String (char arrofchar []): n n Constructs a new string and initializes it to specifies chars[] ={‘a’, ’b’, ’c’} String str =new String (chars) str gets initialized to abc.
n public String(char[] value, int offset, int count) n n Allocates a new String that contains characters from a subarray of the character array argument Char chars[]={‘a’, ’b’, ’c’, ’d’, ’e’, ’f’}; String str=new String(chars 2, 3); Initializes to : cde
Operations on Strings n String literals : Chars enclosed in “ “. n n String Concatenation use + or += String str 1 = “KS School”; String str 2 = “of Business Management”; String str 3 = “KS School”+ “of Business Management”; //Compile time expression String name = “Gujarat”; String str 4 = “Gandhinagar is in ” + name; String str 5 = new String(“Gandhinagar is in Gujarat”); n n String str =“Hello world!“
n n to. String( ): object class has to. String() that returns descriptive string. returns a String object that contains a string. CHARACTER EXTRACTION METHODS char. At(): n This method returns the character located at the String's specified index. n The string indexes start from zero. n Syntax: n public char. At(int index) n Return Value : n Returns a char at the specified index.
getchars() n This method copies characters from this string into the destination character array. n n Syntax: public void get. Chars(int src. Begin, int src. End, char[] dst, int dst. Begin) Return Value : It does not return any value but throws Index. Out. Of. Bounds. Exception
get. Bytes() n Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. n n Syntax: public byte[] get. Bytes() Returns: n The resultant byte array
to. Char. Array() n n n This method converts this string to a new character array. Syntax: public char[] to. Char. Array() Return Value : It returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
String Comparison Methods n n equals(): compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Syntax: public boolean equals(Object an. Object) Return Value : true if the String are equal; false otherwise.
equals. Ignore. Case() n n n compares a String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case. Syntax: public boolean equals. Ignore. Case(String another. String) Return Value : true if the argument is not null and the Strings are equal, ignoring case; false otherwise.
region. Matches() n n This method tests if two string regions are equal. Syntax: public boolean region. Matches(int start. Index, String str 2, int str 2 start. Index, int numchars) or public boolean region. Matches(boolean ignore. Case, int start. Index, String str, int str 2 start. Index, int numchars)
compare. To() n n n The value 0 if strings are equal; The value less than 0 if the argument is a string is greater than other string; The value greater than 0 if the argument is a string less than other string.
starts. With() and ends. With() n Determines if the given string begins with a specified string. Or ends with the specified suffix n n n public boolean starts. With(String prefix, int toffset) Or public boolean starts. With(String prefix) n public boolean ends. With(String suffix) Return Value : n True/False n
length() n n n returns the number of characters contained in the string object. public class String. Demo{ public static void main(String args[]) { String str = “Welcome"; int len = str. length(); System. out. println( "String Length is : " + len ); } }
index. Of() and last. Index. Of() n n n n public int index. Of(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur. public int index. Of(int ch, int from. Index): Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1 if the character does not occur. int index. Of(String str): int index. Of(String str, int from. Index): Syntax: Here is the syntax of this method: public int index. Of(int ch ) ; public int index. Of(int ch, int from. Index) int index. Of(String str) ; int index. Of(String str, int from. Index)
substring() n n substring(int i): This method is used to find all sub string after index i. substring(int start, int end): This is used to find the substring between start and end.
replace() n n n returns a new string resulting from replacing all occurrences of old. Char in this string with new. Char Syntax: public String replace(char old. Char, char new. Char) Return Value : It returns a string derived from this string by replacing every occurrence of old. Char with new. Char.
value. Of() n n It is a static method that is overloaded within String for all of Java's built-in types, so that each type can be converted properly into a string. value. Of( ) is also overloaded for type Object, so an object of any class type you create can also be used. static String value. Of(double num) static String value. Of(long num) static String value. Of(Object ob) static String value. Of(chars[ ])
to. Upper. Case() and to. Lower. Case() n n To convert all lower case characters in a string to upper case, use the method to. Upper. Case() of the String class. To convert all upper case characters in a string to lower case, use the method to. Lower. Case() of the String class.
String. Buffer n n A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified String. Buffer() Constructs a string buffer with no characters in it and an initial capacity of 16 characters. String. Buffer(int length) Constructs a string buffer with no characters in it and an initial capacity specified by the length argument. String. Buffer(String str) Constructs a string buffer so that it represents the same sequence of characters as the string argument
n n length(). Returns the length (character count) of this string buffer. capacity(). Returns the current capacity of the String buffer. set. Length(int new. Length) Sets the length of this String buffer. char. At(int index) The specified character of the sequence in string buffer, as indicated by the index argument, is returned.
n n set. Char. At(int index, char ch) The character at the specified index of this string buffer is set to ch append(String str). Appends the string to this string buffer. insert(int offset, char c). Inserts the string representation of the char argument into this string buffer. reverse() The character sequence contained in this string buffer is replaced by the reverse of the sequence.
n n delete(int start, int end) Removes the characters in a substring of this String. Buffer replace(int start, int end, String str) Replaces the characters in a substring with characters in the specified String.
- Slides: 25