A String is a sequence of character. It is one of most powerful class in java. String object is immutable, It means once you created a string object, Changes to it's contents or length is not possible.
Most frequent way of creating String object is..
String str = "Hello Java";
Here, "Hello Java" is called string literal. When we use String class in our program we should know few points about String in java.
For example.....
char[] data = {'a', 'b', 'c', 'd', 'e'};
String str = new String(data);
By this constructor, A String will be created having string value "abcde".
For example.....
byte[] data = {65, 66, 67, 68, 69, 70};
String str = new String(data);
By this constructor, A String will be created having string value "ABCDE".
For example...
StringBuffer buffer = new StringBuffer();
buffer.append('a');
buffer.append("bcd");
String str = new String(buffer);
By this consturctor, A String will be created having string value "abcd".
For example....
String s1 = "Hiiii Java";
String s2 = new String(s1);
Methods in String class:-
Most frequent way of creating String object is..
String str = "Hello Java";
Here, "Hello Java" is called string literal. When we use String class in our program we should know few points about String in java.
- String consist of sequence of characters. Each character in String is a 16-bit Unicode character.
- "+" Operator is overloaded for String class and hence it can be use for concatenate to Strings in java, For Example
String str = "Hello"+" Java"; - In java a String can be created from a byte array, character array, from existing String, from StringBuffer, or from literal as shown above, Java provides a wide range of constructor for all these.
- Every class has a method i.e toString() to provide string representation of it's object.
- String index is start with 0.
Note:-
- String object is not an array of characters.
- String object is immutable; its reference variable is not.
Constructors in String class:-
String class has several constuctors. some of them we discuss here.
- To ceate a String object from literals we can do this.
- To create a empty String call it's default constructor. For example.
- To create a String object from a character array we can use following form of constructor
For example.....
char[] data = {'a', 'b', 'c', 'd', 'e'};
String str = new String(data);
By this constructor, A String will be created having string value "abcde".
- To create a String object from a byte array we can use following form of constructor
String(byte[] values);
For example.....
byte[] data = {65, 66, 67, 68, 69, 70};
String str = new String(data);
By this constructor, A String will be created having string value "ABCDE".
- To create a String from StringBuffer we can use following form of constructor
For example...
StringBuffer buffer = new StringBuffer();
buffer.append('a');
buffer.append("bcd");
String str = new String(buffer);
By this consturctor, A String will be created having string value "abcd".
- To create a String from existing String, we can use following form of constructor
For example....
String s1 = "Hiiii Java";
String s2 = new String(s1);
- To create a subrange of an character array as an input for making String, we can use following form of constructor
String(char[] values, int starIndex, int numChars);
Here startIndex spedifies the index at which the subrange begins, and numChars specifies the number of charectrer to use
For example.....
char[] data = {'a', 'b', 'c', 'd', 'e', 'f', 'g',' h'};
String str = new String(data, 2, 3);
By this this constructor, A String will be created having string value "cde".
Methods in String class:-
- String concat(String str)
Concatenates the specified string to the end of this string.
- char charAt(int index)
- boolean endsWith(String value)
- boolean equals(Object object)
- boolean equalsIgnoreCase(String object)
- int indexOf(String str, int fromIndex)
- int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
- int indexOf(int ch, int fromIndex)
|
|
No comments:
Post a Comment