30 May 2014
Strings in Java
Strings
- Representing a set of characters altogether, we can define the data type String is used. For example, Following line declares and initialize a String has an initial value “Welcome to Java”
String str = "Welcome to Java";
- String is predefined java class same like System, Scanner etc.
- String is not a primitive data type
- String is a reference type
- A string object is immutable (Mean once object is created, it’s contents are unchange able) For example :
String str = "Java";
str = "HTML";
In the above example : Instead of changing the contents of str, str unrefrenced and updated with a new reference containing “HTML”. - Interned Strings : JVM uses unique instance to string litrals with the same character sequence to improve and utilize memory efficiently. It also helps to save memory.
Result : s1==s2 is false. Using new keyword creates a new object. s1==s3 is true. using the string initializer and without creating a new object and if the same type of interned object (having same sequence of characters) is already created.
How to get String Length, Characters, and Combine Strings?
- length() Returns an int value, have the the total number of characters available in this string
- charAt(int index ) Returns a character of given index from a string
- concat(String str) Generates a new string after concatenating the two ( this string with string str)
How to Convert, Replace, and Splitt Strings?
- toLowerCase() Converts all characters to lowercase and returns a new string
- toUpperCase() Converts all characters to uppercase and returns a new string
- trim() Trimms blank character from both sides and return a new string
- replace(char oChar, char nChar) Replaces all matched character with new characters and returns a new string
- replaceFirst(String oString, String nString) Replaces the first match substring and returns a new string
- replaceAll(String oString, String nString) Replace all matched substring from string and return a new string
- split(String delimiter ) Split the substring by the delimeter and returns an array of strings
How to extract Substrings from a String?
- subString(int beginIndex) Returns this string’s substring that begins with the character at the specified beginIndex and extends to the end of the string
- subString(int beginIndex, int endIndex) Returns this string’s substring that begins at the specified beginIndex and extends to the character at index endIndex – 1. Note that the character at endIndex is not part of the substring.
How to find a Character or a Substring from a String?
- indexOf(char c) Retruns an int value containing idex of the first occurrence of c in the string otherwise -1 if no match is found in this string
- indexOf(char c, int frmIndex) Retruns an int value containing index of the first occurrence of c after given idex in the string otherwise -1 if no match is found in this string
- indexOf(String str ) Returns an int containing the index of the first occurence of string in str otherwise -1 if no match is found in this string
- indexOf(String str, int fromIndex) Returns an int value containing the index of the first occurrence of string s in this str after fromIndex otherwise -1 if no match is found in this string
- lastIndexOf(int c) Returns an int value containing the index of the last occurrence of c in the string otherwise -1 if no match is found in this string
- lastIndexOf( int c, int fromIndex ) Returns an int value containing the index of the last occurrence of c before fromIndex in this string otherwise -1 if no match is found in this string
- lastIndexOf(String s) Returns an int value containing the index of the last occurrence of string sotherwise -1 if no match in this string
- lastIndexOf(String s, int frmIndex ) Returns an int value containing the index of the last occurrence of string s before frmIndex otherwise -1 if no match this string
No Responses