String

String

   public char charAt (int index)
  Returns the char at the specified index. For example, the following code returns ‘J’.
   "Java is cool".charAt (0)
   public String concate (String s)
  Concatenates the specified string to the end of this String and return the new String. For example, "Java". concat("is cool") returns "Java is cool".
   public boolean equals (String anotherString)
  Compares the value of this String and anotherString and returns true if the values match.
   public boolean endsWith (String suffix)
  Tests if this String ends with the specified suffix.
   public int indexOf (String substring)
  Returns the index of the first occurrence of the specified substring. If no match is found, returns −1. For instance, the following code returns 8.
         "Java is cool". indexOf ("cool")
   public int indexOf (String substring, int fromIndex)
  Returns the index of the first occurrence of the specified substring starting from the specified index. If no match is found, returns −1.
   public int last IndexOf (String substring)
  Returns the index of the last occurrence of the specified substring. If no match is found, returns −1.
   public int lastIndexOf (String substring, int fromIndex)
  Returns the index of the last occurrence of the specified substring starting from the specified index. If no match is found, returns −1. For example, the following expression returns 3.
         "Java is cool".lastIndexOf("a")
   public String substring (int beginIndex)
  Returns a substring of the current string starting from the specified index. For instance, "Java is cool". substring(8) returns "cool".
   public String substring (int beginIndex, int endIndex)
  Returns a substring of the current string starting from beginIndex to endIndex. For example, the following code returns "is":
            "Java is cool".substring(5, 7)
   public String replace (char oldChar, char newChar)
  Replaces every occurrence of oldChar with newChar in the current string and returns the new String. For instance, "dingdong".replace(‘d’, ‘k’) returns "kingkong".
   public int length()
  Returns the number of characters in this String. For example, "Java is cool".length() returns 12.
   public String[] split (String regEx)
  Splits this String around matches of the specified regular expression. For example, "Java is cool".split(“ ”) returns an array of three Strings. The first array element is "Java", the second "is", and the third "cool".
   public boolean startsWith (String prefix)
  Tests if the current string starts with the specified prefix.
   public char[] toCharArray()
  Converts this string to an array of chars.
   public String toLowerCase()
  Converts all the characters in the current string to lower case. For instance, "Java is cool".toLowerCase() returns "java is cool".
   public String toUpperCase()
  Converts all the characters in the current string to upper case. For instance, "Java is cool".toUpperCase() returns "JAVA IS COOL".
   public String trim()
  Trims the trailing and leading white spaces and returns a new string. For example, "Java".trim() returns "Java".

In addition, there are two static methods, valueOf and format. The valueOf method converts a primitive, a char array, or an instance of Object into a string representation and there are nine overloads of this method.

   public static String valueOf (boolean value)
   
   public static String valueOf(char value)
   public static String valueOf(char [] value)
   public static String valueOf(char [] value, int offset, int length)
   public static String valueOf(double value)
   public static String valueOf(float value)
   public static String valueOf(int value)
   public static String valueOf(long value)
   public static String valueOf(Object value)

For example, the following code returns the string "23"

   String.valueOf(23);

</code>

String to int

There are also static methods that you can use to parse a String to an int (parseInt) and convert an int to a String (toString). The signatures of the methods are as follows.

     public static int parsetInt (String string)
     public static String toString (int i)

StringBuilder

The StringBuilder class has four constructors. You can pass a java.lang.CharSequence, a String, or an int.

     public StringBuilder()
     public StringBuilder (CharSequence seq)
     public StringBuilder (int capacity)
     public StringBuilder (String string)

If you create a StringBuilder object without specifying the capacity, the object will have a capacity of 16 characters. If its content exceeds 16 characters, it will grow automatically. If you know that your string will be longer than 16 characters, it is a good idea to allocate enough capacity as it takes time to grow a StringBuilder's capacity.
StringBuilder Class's Methods

The StringBuilder class has several methods. The main ones are capacity, length, append, and insert.

     public int capacity()

    Returns the capacity of the StringBuilder object.

     public int length()

    Returns the length of the string the StringBuilder object stores. The value is less than or equal to the capacity of the StringBuilder.

     public StringBuilder append (String string)

    Appends the specified String to the end of the contained string. In addition, append has various overloads that allow you to pass a primitive, a char array, and an java.lang.Object instance. For example, examine the following code.

     StringBuilder sb = new StringBuilder(100);
     sb.append ("Matrix");
     sb.append (2);

    After the last line, the content of sb is "Matrix 2".

    One important point to note is that the append methods return the StringBuilder object itself, the same object on which append is invoked. As a result, you can chain calls to the append methods.

         sb.append ("Matrix").append (2);
     public StringBuilder insert(int offset, String string)

    Inserts the specified string at the position indicated by offset. In addition, insert has various overloads that allow you to pass primitives and a java.lang.Object instance. For example,

     StringBuilder sb2 = new StringBuilder (100);
     sb2. append ("night");
     sb2. insert (0, `k'); // value = "knight"

    Like append, insert also returns the current StringBuilder object, so chaining insert is also permitted.

     public String toString()

    Returns a String object representing the value of the StringBuilder.

Pad

public static String padRight(String s, int n) {
     return String.format("%1$-" + n + "s", s);  
}

public static String padLeft(String s, int n) {
    return String.format("%1$" + n + "s", s);  
}

...

public static void main(String args[]) throws Exception {
 System.out.println(padRight("Howto", 20) + "*");
 System.out.println(padLeft("Howto", 20) + "*");
}
/*
  output :
     Howto               *
                    Howto*
*/
M:/SanDiegoWWW/www/dokuwiki/data/pages/san.java/san.java.string/start.txt · Dernière modification: 2012/12/19 10:19 par admin
 
Sauf mention contraire, le contenu de ce wiki est placé sous la licence suivante : CC Attribution-Noncommercial 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki