KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > String

java.lang
Class String

java.lang.Object
  extended by java.lang.String
All Implemented Interfaces:
Serializable, CharSequence, Comparable<String>
See Also:
Top Examples, Source Code, NullPointerException, Character, Object.toString(), StringBuffer, StringBuilder, Charset

public static final Comparator<String> CASE_INSENSITIVE_ORDER
See Also:
Collator.compare(String, String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1396]String and StringBuffer
By Anonymous on 2005/04/18 13:03:33  Rate
In case anyone has trouble dealing with Strings, Java's String type  
 is for fairly non-changing String data. There aren't any real methods  
 that allow you to cut/copy/paste/alter an object of type String. You  
 are out of luck. It is somewhat of a pain until you understand this  
 difference setup by Java. 
  
  
 An object of type StringBuffer has more methods for string changing.  
 When you are done messing with the StringBuffer object,  ( ie. it's not  
 going to change anymore )  return it as a String type again through  
 your methods with return


public char charAt(int index)
See Also:
IndexOutOfBoundsException, CharSequence
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[132]Get a specific character from a string
By Anonymous on 2005/06/23 19:35:52  Rate
"joo".charAt ( 1 ) 

[851]_
By anwar on 2005/06/23 19:36:07  Rate
string temp = new String  ( "this is string" ) ; 
 System.out.println ( temp.charAt ( 0 )  ) ; 
 //the output will be "t" from "this"


public int codePointAt(int index)
See Also:
IndexOutOfBoundsException, length()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int codePointBefore(int index)
See Also:
IndexOutOfBoundsException, length
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int codePointCount(int beginIndex,
                          int endIndex)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int compareTo(Object o)
See Also:
Comparable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[611]Upcast to Object
By sivaa_java { at } rediffmail { dot } com on 2005/04/28 04:31:44  Rate
"sample".compareTo (  ( Object ) "sample" ) ;

[1403]_
By Anonymous on 2005/07/25 07:21:55  Rate
The upcast to Object in the above example is not necessary.

public int compareTo(String anotherString)
See Also:
Comparable, compareTo, equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int compareToIgnoreCase(String str)
See Also:
Collator.compare(String, String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[902]Comparation without regard to the character cases
By Melvin Ni??al on 2005/04/05 12:46:34  Rate
class Compare 
  {  
     public tatic void main ( String args [  ]  )  
      {  
           String s1 = "Hello"; 
           String s2 = "HeLLO"; 
           if ( s1.compareToIgnoreCase ( s2 )  )  
               System.out.print ( "equal" ) ; 
           else 
               System.out.print ( "not equal" )  
      }  
  }  
  
  
  
  
 /** 
 The output would be  
          equals 
  
  
 since s1 and s2 values are compared without regard to the character cases. 
 */


[1253]_
By Anonymous on 2005/01/14 03:06:12  Rate
The previous example would have been OK had it been correct. compareToIgnoreCase (  )  returns an int, therefore the example should read: 
  
  
 ... 
           if ( s1.compareToIgnoreCase ( s2 )  == 0 )  
 ... 
  
  
 and don't forget the 's' in front of 'tatic' ;- )  
  
  
 


public String concat(String str)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[612]Combine string
By sivaa_java { at } rediffmail { dot } com on 2004/10/13 20:28:34  Rate
public class Combine 
  {  
   public static void main ( String args [  ]  )  
    {  
      String s="Java"; 
      String s1=" Programme"; 
      System.out.println ( "The Combine String is : "+s.concat ( s1 )  ) ; 
     }  
  } 


[1351]_
By Rebecca on 2005/08/25 13:24:54  Rate
public class She  {  
   protected String gender = "female"; 
    
   public static void main ( String [  ]  args )   {  
     She myGirl = new She  (  ) ; 
     System.out.println ( "Gender: " .concat ( myGirl.getGender (  )  )  ) ; 
  
  
    }  
    
   public String getGender (  )   {  
     return gender; 
    }  
   public void setGender ( String string )   {  
     gender = string; 
    }  
  }  
 


public boolean contains(CharSequence s)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contentEquals(CharSequence cs)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contentEquals(StringBuffer sb)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String copyValueOf(char[] data)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String copyValueOf(char[] data,
                                 int offset,
                                 int count)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean endsWith(String suffix)
See Also:
equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1179]String endsWith
By s_bui99 { at } yahoo { dot } com on 2004/12/07 09:14:14  Rate
s1 = new String ( "This is just a test." ) ; 
 String search1 = "test"; 
 String search2 = "test."; 
 System.out.println ( "String s1: " + s1 ) ; 
 System.out.println ( "Search string1: " + search1 ) ; 
 boolean status = s1.endsWith ( search1 ) ; 
 System.out.println ( " Status of search: " +  ( status ? "Found" : "Not Found" )  ) ; 
 System.out.println ( " Search string2: " + search2 ) ; 
 status = s1.endsWith ( search2 ) ; 
 System.out.println ( " Status of search: " +  ( status ? "Found" : "Not Found" )  ) ; 
  
  
 RESULT: 
 ======= 
 String s1: This is a test. 
 Search string1: test 
 Status of search: Not Found 
 Search string2: test. 
 Status of search: Found


public boolean equals(Object anObject)
See Also:
equalsIgnoreCase(java.lang.String), compareTo(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1424]String equals
By Santosh Kumar on 2005/05/13 00:54:05  Rate
 
 if  (  ( null != str1 )  &&  ( str1.equals ( str2 )  )  
    System.out.print ( "equals" ) ; 
 else  System.out.print ( "Not equals" ) ; 
 


[2021]proxy
By berhanucs { at } gmail { dot } com on 2014/05/29 02:11:07  Rate
hi proxy server you sggh

public boolean equalsIgnoreCase(String anotherString)
See Also:
Character.toUpperCase(char), Character.toLowerCase(char), equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[322]String equalsIgnoreCase
By Ramakrishna Alakunta on 2003/07/22 08:58:06  Rate
String statusCode = "RAMAKRISHNA"; 
 if ( statusCode.equalsIgnoreCase ( "Ramakrishna" )  )  {  
   System.out.println ( "Correct" ) ; 
  } else {  
   System.out.println ( "Wrong" ) ; 
   
  } 


public static String format(String format,
                            Object... args)
See Also:
Formatter, NullPointerException, Locale.getDefault()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1803]sprintf in Java
By ab on 2006/08/06 20:04:51  Rate
// sprintf in Java  ( since 1.5 )  
 int i=99; 
 int j=255; 
 System.out.println ( String.format ( "i: %04d", i )  ) ; // with leading zeros 
 System.out.println ( String.format ( "j: dec: %d , hex: %x", j, j )  ) ; // hex 
 // etc...


public static String format(Locale l,
                            String format,
                            Object... args)
See Also:
Formatter, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public byte[] getBytes()
See Also:
CharsetEncoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[705]Get each byte of a string
By vellopop on 2005/05/24 00:09:54  Rate
String d1 = " cool "; 
 byte buf  [  ]  = d1.getBytes (  ) ; 
 for  ( int i=0; i < buf.length; i++ )  
  {  
     System.out.println ( " Index " + i + "is : " + buf [ i ]  ) ; 
  }  
 


@Deprecated
public void getBytes(int srcBegin,
                                int srcEnd,
                                byte[] dst,
                                int dstBegin)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public byte[] getBytes(String charsetName)
                throws UnsupportedEncodingException
See Also:
charset, CharsetEncoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1616]Simiple checksum generator
By Anonymous on 2005/11/04 20:23:08  Rate
/** 
 * Simiple checksum generator 
 */
 
 short checksum  ( String value )  
  {  
  
  
 short checksum = STARTING_VALUE; 
 short multiplier = 1; 
  
  
 short STARTING_VALUE = -631; 
 String ENCODING = "UTF-8"; 
  
  
 try 
  {  
     byte [  ]  data = value.getBytes  ( ENCODING ) ; 
     for  ( int b = 0; b  <  data.length; ++b )  
   checksum += data [ b ]  * multiplier++; 
  }  
 catch  ( java.io.UnsupportedEncodingException ex )  
  {  
     ex.printStackTrace (  ) ; 
  }  
  
  
 return checksum; 
  
  
  } 


public void getChars(int srcBegin,
                     int srcEnd,
                     char[] dst,
                     int dstBegin)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1180]Use getChars() to copy from one string to another string
By s_bui99 { at } yahoo { dot } com on 2004/12/07 13:19:53  Rate
//Use getChars (  )  to copy from one string to another string: 
 src = "Good Morning! How are you?"; 
 char dest [  ]  = new char [ src.length (  )  ] ; 
 src.getChars ( 0, src.length (  ) , dest, 0 ) ; 
 System.out.println ( " Source string: " + src ) ; 
 System.out.print ( " Destination string: " ) ; 
 for  ( int i = 0; i  <  dest.length; i++ )  
    System.out.print ( dest [ i ]  ) ; 
  
  
 RESULT: 
 ======= 
    Source string: Good Morning! How are you? 
    Destination string: Good Morning! How are you? 
 


public int hashCode()
See Also:
Hashtable, Object.equals(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int indexOf(int ch)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1218]Case-insensitive implementation of indexOf
By Anonymous on 2005/02/11 13:34:51  Rate
All the IndexOf methods do case-sensitive comparation.   
  
  
 Here is case-insensitive implementation, 
  
  
 public static int IgnoreCaseIndexOf ( String mainString, String str, int fromIndex )   {  
   String s1 = mainString.toLowerCase (  ) ; 
   String t1 = str.toLowerCase (  ) ; 
  
  
   return s1.indexOf ( t1, fromIndex ) ; 
  }  
  
  
  
   return s1.indexOf ( t1, startPos ) ; 
  }  
 


public int indexOf(int ch,
                   int fromIndex)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int indexOf(String str)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[21]_
By Max Okist on 2005/03/16 14:18:42  Rate
String gender = "female"; 
 if  ( gender.indexOf ( "fem" )  != -1 )   {  
    System.out.println ( "The gender is female." ) ; 
  }  else  {  
    System.out.println ( "The gender is not female." ) ; 
  }  
  
  
 


[725]dontRepeat
By sandeshj { at } anz { dot } com on 2004/04/08 03:15:53  Rate
public class dontRepeat 
  {  
   public static void main ( String args [  ]  )  
    {  
     String str1 [  ] = { "01","01","02","03","04","05","02","02","05","09","01","10" } ; 
     String str2=new String (  ) ; 
     for  ( int i=0; i < str1.length ; i++ )  
      {  
       if ( str2.indexOf ( str1 [ i ]  ) ==-1 )  
        {  
         str2=str2+str1 [ i ] +":"; 
         System.out.println ( str1 [ i ]  ) ; 
        }  
      }  
     System.out.println ( str2 ) ; 
    }  
  } 


public int indexOf(String str,
                   int fromIndex)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String intern()
See Also:
equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[203]When use String as a synchronized object
By Anonymous on 2004/07/20 09:04:55  Rate
//You need to retrieve the canonical representation of  
 //the String object with the String.intern method. when used 
 //as a synchronized object 
  
  
  
 USE 
  
  
 synchronized  ( aString.intern (  )  )   
  
  
 NOT 
  
  
  
 synchronized  ( aString )  


public int lastIndexOf(int ch)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[308]Get filename ext
By Anonymous on 2004/12/08 06:01:48  Rate
String sFileName = "ThisIsAWordFile.doc"; 
 String sFileExt = sFileName.substring ( sFileName.lastIndexOf ( '.' )  + 1 ) ; 
 System.out.println (  sFileExt ) ; 
 


public int lastIndexOf(int ch,
                       int fromIndex)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int lastIndexOf(String str)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int lastIndexOf(String str,
                       int fromIndex)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int length()
See Also:
CharSequence
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[570]String length
By VentureKing on 2003/12/19 16:17:16  Rate
String strName = "This string should have a length of 39."; 
 System.out.println ( strName.length (  )  ) ;


public boolean matches(String regex)
See Also:
Pattern, PatternSyntaxException, matches
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[216]Implement grep
By Anonymous on 2004/10/04 10:52:44  Rate
/***************************************************/ 
 // Usage: java grep "regex" filename 
 import java.io.*; 
  
  
 public class grep  {  
  public static void main ( String [  ]  args )   {  
     try  {  
        BufferedReader in = new BufferedReader ( new FileReader ( args [ 1 ]  )  ) ; 
        String foo; 
        while  (  ( foo = in.readLine (  )  )  != null )   {  
           if  ( foo.matches ( args [ 0 ]  )  )   {  
              System.out.println ( foo ) ; 
            }  
         }  
      }  catch ( IOException e )   {  
        System.err.println ( "ERROR in file I/O:" ) ; 
        e.printStackTrace (  ) ; 
      }  
   }  
  }  
 


public int offsetByCodePoints(int index,
                              int codePointOffset)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean regionMatches(boolean ignoreCase,
                             int toffset,
                             String other,
                             int ooffset,
                             int len)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[114]To know if a String is part of another String using region match
By Yag on 2005/02/04 17:57:42  Rate
String t = "ABC WINDOWS TEST"; 
 t.regionMatches ( true, 0, "windows", 0, 7 ) ; 
 


[230]_
By knocte { at } hotmail { dot } com on 2005/02/04 17:58:04  Rate
String t = "ABC WINDOWS TEST"; 
 t.regionMatches ( true, 4, "windows", 0, 7 ) ; 
  
  
 And to know if a String is part of another String, take this algorithm: 
  
  
   public boolean IsXPartOfY ( String X, String Y )  {  
     boolean found = false; 
     int limit =  ( Y.length (  )  - X.length (  )  + 1 ) ; 
  
  
     if  ( limit  >  0 )  {  
       int i=0; 
       while  (  ( i  <  limit ) && ( !found )  )  {  
         if  ( Y.regionMatches ( true, i, X, 0, X.length (  )  )  )  
           found = true; 
         else 
           i++; 
        }  
      }  
  
  
     return found; 
    }  
 


public boolean regionMatches(int toffset,
                             String other,
                             int ooffset,
                             int len)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String replace(char oldChar,
                      char newChar)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[552]Replaces ALL occurrences of a char
By Anonymous on 2003/12/02 06:04:15  Rate
String date = "2003-12-02"; 
 String newDate = date.replace ( '-','\u0000' ) ;


[1217]_
By Anonymous on 2005/04/12 05:57:18  Rate
Please NOTE: it replaces ALL occurrences of oldChar in this string with newChar.

public String replace(CharSequence target,
                      CharSequence replacement)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String replaceAll(String regex,
                         String replacement)
See Also:
Pattern, PatternSyntaxException, replaceAll, matcher, compile
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[242]Replace all occurrences of a word
By Anonymous on 2004/11/18 21:04:25  Rate
// Use a String convenience method to replace all occurrences of the word 
 // "java"  ( with any capitalization )  with the correctly capitalized "Java" 
 s = s.replaceAll ( " ( ?i ) \\bjava\\b", // The pattern: "java", case-insensitive 
                  "Java" ) ; // The replacement string


[412]_
By marinus snijder on 2004/04/29 08:23:14  Rate
public static synchronized String replaceAll ( String str, String pattern, String replace )   {  
   StringBuffer lSb = new StringBuffer (  ) ; 
   if  (  ( str != null )  &&  ( pattern != null )  &&  ( pattern.length (  )   >  0 )  &&  ( replace != null )  )   {  
     int i = 0;  
     int j = str.indexOf ( pattern, i ) ; 
     int l = pattern.length (  ) ; 
     int m = str.length (  ) ; 
     if  ( j  >  -1 )   {  
       while  ( j  >  -1 )   {       
         if  ( i != j )   {  
           lSb.append ( str.substring ( i, j )  ) ;  
          }  
         lSb.append ( replace ) ;       
         i = j + l; 
         j =  ( i  >  m )  ? -1 : str.indexOf ( pattern, i ) ; 
        }  
       if  ( i  <  m )   {  
         lSb.append ( str.substring ( i )  ) ; 
        }  
      }  
     else  {  
       lSb.append ( str ) ; 
      }  
    }  
   return lSb.toString (  ) ; 
  } 


[1360]_
By fred on 2005/03/24 12:59:57  Rate
import java.io.*; 
 import java.text.*; 
 import java.util.regex.*; 
  
  
 // Just change the line below to the name of the file here DateManips.java 
 public class stringReplace  {   
  
  
   public static void main  ( String [  ]  args )   {  
      
     String testString = "12 345"; 
      
     System.out.println ( testString ) ; 
      
     String outString = testString.replaceAll ( "\\s","" ) ; 
      
     System.out.println ( outString ) ; 
            
            
            
          }   
       
  } 


public String replaceFirst(String regex,
                           String replacement)
See Also:
Pattern, PatternSyntaxException, replaceFirst, matcher, compile
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[359]Search and replace
By ksrihari { at } chn { dot } cognizant { dot } com on 2003/08/20 02:31:48  Rate
import java.io.*; 
 import java.lang.String; 
  
  
 public class Search2 
  {  
   public static void main ( String args [  ]  )  
    {  
  
  
     try 
      {  
       BufferedReader br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( "c:/Work/AMTI450" )  )  ) ; 
       BufferedWriter wbuf=null; 
       wbuf = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( "c:/Work/test1" )  )  ) ; 
       String s; 
       String s1 = null; 
       while (  ( s = br.readLine (  )  ) !=null )  
        {  
           if  ( s.substring ( 6,7 ) .equals ( "C" )  )  
          {  
  
  
           s1 = s.replaceFirst ( "C"," " ) ; 
  
  
            }  
           else 
            {  
             s1 = s; 
             }  
         wbuf.write ( s1 ) ; 
           wbuf.write ( "\n" ) ; 
  
  
        }  
  
  
       br.close (  ) ; 
  
  
       wbuf.close (  ) ; 
      }  
     catch ( IOException e )  
      {  
       System.err.println ( "Error reading from file " + args [ 0 ]  + ": " + e ) ; 
       System.exit ( 1 ) ; 
      }  
    }  
  } 


public String[] split(String regex)
See Also:
Pattern, PatternSyntaxException, split
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[353]Splitting String
By Anonymous on 2005/10/05 08:03:01  Rate
String src = "a,b,c,d,e,f,g"; 
 String s [  ]  = src.split ( "," ) ; 
  
  
 for  ( int i = s.length - 1; i  > =0; i -- )   {  
     System.out.println ( s [ i ]  ) ; 
  }  
 


[1821]An interesting thing about String.split()
By Anonymous on 2006/09/20 08:06:35  Rate
" s".split ( " " )      - >   { "","","s" }  
 "".split ( ""  )         - >   { "" }  
 " ".split ( " " )        - >   {  }   ( ! )  
 " ".split ( " " )  - >   {  }   ( ! )  
 " s ".split ( " " )     - >   { "","","s" }   ( ! ) 


[1822]regex expression in String.Split
By Anonymous on 2006/09/20 08:07:55  Rate
split (  )  is based on regex expression, a special attention is needed with some characters which have a special meaning in a regex expression. 
  
  
 For example : 
  
  
 String s3 = "Real.How.To"; 
 ... 
 temp = s3.split ( "\\." ) ; 
  
  
 or 
  
  
 String s3 = "Real|How|To"; 
 ... 
 temp = s3.split ( "\\|" ) ; 
  
  
 The special character needs to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !


public String[] split(String regex,
                      int limit)
See Also:
Pattern, PatternSyntaxException, split, compile
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean startsWith(String prefix)
See Also:
equals(Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[571]String startsWith
By VentureKing on 2003/12/19 16:24:27  Rate
String str1 = "Starting of string"; 
 String str2 = "Beginning of string"; 
  
  
 if  ( str1.startsWith ( "Start" )  )   {  
   System.out.println ( "str1 starts with \"Start\"" ) ; 
  }  
 else  {  
   System.out.println ( "str1 does NOT start with \"Start\"" ) ; 
  }  
  
  
 if  ( str2.startsWith ( "Start" )  )   {  
   System.out.println ( "str2 starts with \"Start\"" ) ; 
  }  
 else  {  
   System.out.println ( "str2 does NOT start with \"Start\"" ) ; 
  } 


public boolean startsWith(String prefix,
                          int toffset)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[572]_
By VentureKing on 2003/12/19 16:28:15  Rate
String str1 = "Starting of string"; 
 String str2 = "Beginning of string"; 
 int startPos = 2; 
  
  
 if  ( str1.startsWith ( "art", startPos )  )   {  
   System.out.println ( "str1 starts with \"art\" at character " + startPos ) ; 
  }  
 else  {  
   System.out.println ( "str1 does NOT start with \"art\" at character " + startPos ) ; 
  }  
  
  
 if  ( str2.startsWith ( "art", startPos )  )   {  
   System.out.println ( "str2 starts with \"art\" at character " + startPos ) ; 
  }  
 else  {  
   System.out.println ( "str2 does NOT start with \"art\" at character " + startPos ) ; 
  } 


public String()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[518]Char array to string
By Anonymous on 2003/11/16 04:34:49  Rate
char passchars [  ]  =  {  '\u0114', '\u0135', '\u012D', '\u014F', '\u0100', '\u0162', '\u010A', '\u0148', '\u0142'  } ; 
         String cryptpass = new String ( passchars ) ; 
 


public String(byte[] bytes)
See Also:
CharsetDecoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public String(byte[] ascii,
                         int hibyte)
See Also:
String(byte[]), String(byte[], java.lang.String), String(byte[], int, int), String(byte[], int, int, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(byte[] bytes,
              int offset,
              int length)
See Also:
IndexOutOfBoundsException, CharsetDecoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1900]Convert byte[] to string
By Anonymous on 2007/06/30 09:55:14  Rate
byte [  ]  buffer = readSomeNMEALine (  ) ; 
 String line = new String ( buffer ) ; 
  
  
 or even simpler 
  
  
 StringBuffer buff = new StringBuffer  (  )  
 while  (   ( ch = in.read (  )  )  != '\n' )   {  
  buff.append (  ( char ) ch ) ; 
  }  
 String line = buff.toString (  ) ; 
 


@Deprecated
public String(byte[] ascii,
                         int hibyte,
                         int offset,
                         int count)
See Also:
String(byte[]), String(byte[], java.lang.String), String(byte[], int, int), String(byte[], int, int, java.lang.String), String(byte[], int), IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(byte[] bytes,
              int offset,
              int length,
              String charsetName)
       throws UnsupportedEncodingException
See Also:
IndexOutOfBoundsException, charset, CharsetDecoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(byte[] bytes,
              String charsetName)
       throws UnsupportedEncodingException
See Also:
charset, CharsetDecoder
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(char[] value)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[204]Char array and string
By Anonymous on 2003/03/26 19:06:33  Rate
String str = "abc"; 
   
 is equivalent to:  
  
  
 char data [  ]  =  { 'a', 'b', 'c' } ; 
 String str = new String ( data ) ;


public String(char[] value,
              int offset,
              int count)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(int[] codePoints,
              int offset,
              int count)
See Also:
IndexOutOfBoundsException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(String original)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(StringBuffer buffer)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String(StringBuilder builder)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public CharSequence subSequence(int beginIndex,
                                int endIndex)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String substring(int beginIndex)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String substring(int beginIndex,
                        int endIndex)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[292]Sub string
By dmctavish { at } sandvine { dot } com on 2003/07/04 11:30:04  Rate
String str = "Hello World!"; 
 String world = str.substring ( 6, 11 ) ; 
 System.out.println ( world ) ;


[1791]Removing Comma from a String using substring
By RM on 2006/07/24 01:44:07  Rate
 
 public class SubStringSample  
  {  
  
  
   static String str = "java,"; 
    
    
   public static void main ( String [  ]  args )   
    {      
     String substr = str.substring ( 0,str.lastIndexOf ( "," )  ) ; 
     System.out.println ( substr ) ; 
  
  
    }  
  
  
  }  
 


public char[] toCharArray()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[708]String to char array
By rj_edhan { at } yahoo { dot } com on 2004/10/19 13:41:52  Rate
class toCharArray_Example  {  
   public static void main ( String [  ]  args )   {  
     String objStr = new String ( "This is the test string" ) ; 
     char [  ]  c = objStr.toCharArray (  ) ; 
     for ( int i = 0; i  < = c.length; i++ )  
       System.out.println ( c [ i ]  ) ; 
    }  
  } 


[1466]_
By bv on 2005/06/29 13:39:09  Rate
out put will be the following..This is a useful method to split the Sting in to separate chars for processing each character.I plan to use this for CheckSum calculations.. 
  
  
 T 
 h 
 i 
 s 
   
 i 
 s 
   
   
   
 t 
 h 
 e 
   
   
   
   
   
   
 t 
 e 
 s 
 t 
   
   
   
 s 
 t 
 r 
 i 
 n 
 g


public String toLowerCase()
See Also:
toLowerCase(Locale)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1194]Change case
By ramu_krishna14 { at } yahoo { dot } com on 2005/01/14 21:29:03  Rate
public class toupper  {  
  
  
   public static void main ( String [  ]  args )   {  
     String str = "java"; 
     String str1 = str.toUpperCase (  ) ; 
     System.out.println ( str1 ) ; 
    }  
  } 


[1195]_
By ramu_krishna14 { at } yahoo { dot } com on 2004/12/14 05:53:44  Rate
public class toupper  {  
  
  
   public static void main ( String [  ]  args )   {  
     String str = "JAVA"; 
     String str1 = str.toLowerCase (  ) ; 
     System.out.println ( str1 ) ; 
    }  
  } 


public String toLowerCase(Locale locale)
See Also:
toUpperCase(Locale), toUpperCase(), toLowerCase(), Character
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[918]_
By kalyan on 2004/10/19 04:05:58  Rate
String str1="UPPER"; 
 String str2=str1.toLowerCase (  ) ; 
 System.out.println ( "lower case"+str2 ) ;


public String toString()
See Also:
Object, CharSequence
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[785]Testing toString
By rj_edhan { at } yahoo { dot } com on 2004/05/17 07:45:29  Rate
class Testing_toString  {  
   int i; 
          
         // toString (  )  returns strings  
   public String toString (  )   {  
     return "x is: " + i; 
    }  
  }  
  
  
 public class Testing  {  
   public static void main ( String [  ]  args )   {  
     TestingA objA = new TestingA (  ) ; 
     String test; 
     test = objA.toString (  ) ; 
     System.out.println ( test ) ; 
    }  
  } 


[1859]to string conversion
By Email: krishna_java { at } rediggmail { dot } com on 2007/01/08 19:22:43  Rate
class Test_toString 
  {  
     
     public static void main ( String  [  ] args )  
  
  
    {  
                     
                 // Assign the integer value to k    
                 int k=1234; 
                 // convert the int value to Integer Object   
     Integer b=new Integer ( k ) ; 
     // Convert the integer Object to String using toString (  )  
           System.out.println ( ""+b.toString (  )  ) ; 
  
  
    }  
  } 


public String toUpperCase()
See Also:
toUpperCase(Locale)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[36]convert lower case letter in the string to upper case letter
By pan_khandar { at } yahoo { dot } co { dot } uk on 2005/04/05 04:10:54  Rate
//convert lower case letter in the string to upper case letter 
 String str = "abCd"; 
 System.out.println ( str.toUpperCase (  )  ) ;


[1762]upper case conversion
By neetakushwaha { at } yahoo { dot } com on 2006/05/30 22:44:34  Rate
package com.ibm.developerworks.wspattern.two.commands; 
  
  
 import com.ibm.developerworks.wspattern.two.Command; 
 import com.ibm.developerworks.wspattern.two.CommandModel; 
  
  
 public class UppercaseCommand  
   implements Command  {  
  
  
   public CommandModel execute ( CommandModel model )   {    
     if  ( ! ( model instanceof CaseCommandModel )  )   {  
       throw new IllegalArgumentException ( "Invalid command model" ) ; 
      }  else  {  
       CaseCommandModel umodel =  ( CaseCommandModel ) model; 
       if  ( umodel.getString (  )  == null )   {  
         throw new IllegalArgumentException ( "Invalid command model" ) ; 
        }  else  {  
         umodel.setString ( umodel.getString (  ) .toUpperCase (  )  ) ; 
        }  
       return umodel;  
      }  
    }  
  }  
 


public String toUpperCase(Locale locale)
See Also:
toLowerCase(Locale), toLowerCase(), toUpperCase(), Character
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String trim()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[414]Trim string
By Anonymous on 2004/12/14 05:56:25  Rate
String zz=" sdfdsf"; 
 String zzz= zz.trim (  ) ;


[1371]_
By Roger on 2005/04/13 16:50:08  Rate
String s1 = " Hello World "; 
 String s2 = s1.trim (  ) ; 
 // s2 is "Hello World";


public static String valueOf(boolean b)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(char c)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(char[] data)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1469]Char to string
By innovater { at } web { dot } de on 2005/07/01 07:44:55  Rate
  public static void main ( String  [  ] args )  {  
       char e =  ( char ) 0x03; 
       String end = String.valueOf ( e ) ; 
       System.out.println ( " >  > "+end+" <  < " ) ; 
    }  
  
  
 Output: 
  >  >  <  < 


public static String valueOf(char[] data,
                             int offset,
                             int count)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(double d)
See Also:
Double.toString(double)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(float f)
See Also:
Float.toString(float)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(int i)
See Also:
Integer.toString(int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[6]casting primitives to String
By shahar on 2005/02/13 10:43:36  Rate
//casting primitives to String 
 int i = 17; 
 // WASTFULL : create 2 objects that are thrown to the GC right away 
 System.out.println ( i+"" ) ; 
 // MUCH BETTER 
 System.out.println ( String.valueOf ( i )  ) ; 
 


public static String valueOf(Object obj)
See Also:
Object.toString()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String valueOf(long l)
See Also:
Long.toString(long)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags