String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2,3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.
The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer
class and its append
method. String conversions are implemented through the method toString
, defined by Object
and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
package java.lang; import java.util.Hashtable; import java.util.Locale; import sun.io.ByteToCharConverter; import sun.io.CharToByteConverter; import java.io.CharConversionException; import java.io.UnsupportedEncodingException; (>JDK1.0)public final class String implements java.io.Serializable { // Constructeurs publiques (>JDK1.0) public String(); // obsolète (>JDK1.0) public String(byte ascii[], int hibyte); // obsolète (>JDK1.0) public String(byte ascii[], int hibyte, int offset, int count); (>JDK1.1) public String(byte bytes[]); (>JDK1.1) public String(byte bytes[], String enc) throws UnsupportedEncodingException; (>JDK1.1) public String(byte bytes[], int offset, int length); (>JDK1.1) public String(byte bytes[], int offset, int length, String enc) throws UnsupportedEncodingException; (>JDK1.0) public String(char value[]); (>JDK1.0) public String(char value[], int offset, int count); (>JDK1.0) public String(String value); (>JDK1.0) public String(StringBuffer buffer); // Méthodes de classe publiques (>JDK1.0) public static String copyValueOf(char data[]); (>JDK1.0) public static String copyValueOf(char data[], int offset, int count); (>JDK1.0) public static String valueOf(boolean b); (>JDK1.0) public static String valueOf(char c); (>JDK1.0) public static String valueOf(char data[]); (>JDK1.0) public static String valueOf(char data[], int offset, int count); (>JDK1.0) public static String valueOf(double d); (>JDK1.0) public static String valueOf(float f); (>JDK1.0) public static String valueOf(int i); (>JDK1.0) public static String valueOf(long l); (>JDK1.0) public static String valueOf(Object obj); // Méthodes d'instance publiques (>JDK1.0) public char charAt(int index); (>JDK1.0) public int compareTo(String anotherString); (>JDK1.0) public String concat(String str); (>JDK1.0) public boolean endsWith(String suffix); (>JDK1.0) public boolean equals(Object anObject); (>JDK1.0) public boolean equalsIgnoreCase(String anotherString); (>JDK1.1) public byte[] getBytes(); // obsolète (>JDK1.0) public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin); (>JDK1.1) public byte[] getBytes(String enc) throws UnsupportedEncodingException; (>JDK1.0) public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin); (>JDK1.0) public int hashCode(); (>JDK1.0) public int indexOf(int ch); (>JDK1.0) public int indexOf(int ch, int fromIndex); (>JDK1.0) public int indexOf(String str); (>JDK1.0) public int indexOf(String str, int fromIndex); (>JDK1.0) public native String intern(); (>JDK1.0) public int lastIndexOf(int ch); (>JDK1.0) public int lastIndexOf(int ch, int fromIndex); (>JDK1.0) public int lastIndexOf(String str); (>JDK1.0) public int lastIndexOf(String str, int fromIndex); (>JDK1.0) public int length(); (>JDK1.0) public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len); (>JDK1.0) public boolean regionMatches(int toffset, String other, int ooffset, int len); (>JDK1.0) public String replace(char oldChar, char newChar); (>JDK1.0) public boolean startsWith(String prefix); (>JDK1.0) public boolean startsWith(String prefix, int toffset); (>JDK1.0) public String substring(int beginIndex); (>JDK1.0) public String substring(int beginIndex, int endIndex); (>JDK1.0) public char[] toCharArray(); (>JDK1.1) public String toLowerCase(Locale locale); (>JDK1.0) public String toLowerCase(); (>JDK1.0) public String toString(); (>JDK1.1) public String toUpperCase(Locale locale); (>JDK1.0) public String toUpperCase(); (>JDK1.0) public String trim(); } |
Christophe Merlet redfox@redfoxcenter.org | ©Tous droits réservés
11 septembre 1998 |