Posts

Conversion between Strings and Arrays

Strings are not arrays, but a string can be converted into an array, and vice versa. To convert a string to an array of characters, use the toCharArray method. For example, the following statement converts the string "Java" to an array. char[] chars="Welcome to Java".toCharArray(); So chars[0] is 'J', chars[1] is 'a', chars[2] is 'v', and chars[3] is 'a' and so on... You can also use the getChars(int srcBegin, int srcEnd, char[] dst, int dst- Begin) method to copy a substring of the string from index srcBegin to index srcEnd-1 into a character array dst starting from index dstBegin. For example, the following code copies a substring "to java" in "Welcome to java" from index 7 to str.lenth()-1 into the character array dst starting form index 0. import java.lang.String; class stringtochars{                                 public static void main(String[] arg){           ...

Finding a Character or a Substring

The String class provides several overloaded indexOf and lastIndexOf methods to find a character or a substring in a string. Method syntax Return type Description +indexOf(ch:char) int Returns the index of the first occurrence of ch in the string. Returns-1 if not matched. +indexOf(ch:char,formIndex:int) int Return the index of the first occurrence of ch after fromIndex in the string. Return -1 in not matched. +indexOf(str:string) int Returns the index of the first occurrence of string s in this string. Returns-1 if not matched. +indexOf(str:string,fromIndex:int) int Returns the index of the first occurrence of string s in this string after fromIndex. Returns -1 if not matched. +lastIndexOf(ch:char) int Returns the index of the last occurrence of ch in the string. Returns-1 if not matched.  +lastIndexOf(ch:char,fromIndex:int) int Returns the index of the last occurrence of ch before fromIndex in this string. Returns -1 if not matched.  ...

+split(delimeiter:String);

Return type String[] (Array of string). Returns an array of strings consisting of the substrings split by the delimiter. import  java.lang.String; class split{                      public static void main(String[] arg){                       String[] str="Welcome_to_Java".split("_",0);                      for(int i=0;i<str.length;i++)                             System.out.println(str[i]);                                           } } Output:   Welcome               to              Java

+replaceAll(old string:string,newstring:string);

Return type String. Returns a new string that replaces all matching substrings in this string with the new substring. import  java.lang.String; class replaceAll{                      public static void main(String[] arg){                       System.out.println("Welcome : "+"Welcome".replaceAll("e","EL"));                      } } Output:   Welcome : WELlcomEL

+replaceFirst(OldString:string,newString:string);

Return type String Returns a new string that replaces the first matching substring in this string with the new substring. import java.lang.String; class replacefirst{                             public static void main(String[] args){                             System.out.println("Welcome : ","Welcome".replaceFirst("e","E"));                             } } Output :    Welcome : WElcome

+replace(oldchar,newchar);

Return type String Returns a new string that replaces all matching characters in this string with the new character. import  java.lang.String; class replace{                      public static void main(String[] arg){                       System.out.println("Welcome : "+"Welcome".replace('e','E'));                      } } Output:   Welcome : WElcomE

+trim()

Return type String Returns a new string with blank characters trimmed on both sides. import java.lang.String; class trim{                  public static void main(String[] arg){                  System.out.pringln("    Welcome    :"+"     Welcome     ".trim());                  } } Output:       Welcome     :Welcome