1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 import java.io.UnsupportedEncodingException;
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date September 4, 1998.
16 /* Written using "Java Class Libraries", 2nd edition, plus online
17 * API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Complete to 1.1, but see FIXMEs. Also see testsuite results.
21 public final class String
24 private int boffset; // Note this is a byte offset - don't use in Java code!
32 public String (String value)
35 boffset = value.boffset;
39 public String (StringBuffer buffer)
44 init (buffer.value, 0, buffer.count, true);
48 public String (char[] data)
50 init(data, 0, data.length, false);
53 public String (char[] data, int offset, int count)
55 init(data, offset, count, false);
58 public String (byte[] byteArray)
60 this (byteArray, 0, byteArray.length);
63 public String (byte[] byteArray, int offset, int count)
67 init (byteArray, offset, count,
68 System.getProperty("file.encoding", "8859_1"));
70 catch (UnsupportedEncodingException x1)
72 // Maybe the default encoding is bad.
75 init (byteArray, offset, count, "8859_1");
77 catch (UnsupportedEncodingException x2)
79 // We know this can't happen.
84 public String (byte[] byteArray, String enc)
85 throws UnsupportedEncodingException
87 this (byteArray, 0, byteArray.length, enc);
90 public String (byte[] byteArray, int offset, int count, String enc)
91 throws UnsupportedEncodingException
93 init (byteArray, offset, count, enc);
96 public static String copyValueOf(char[] data)
98 return copyValueOf (data, 0, data.length);
101 public static String copyValueOf(char[] data, int offset, int count)
103 String r = new String ();
104 r.init(data, offset, count, false);
109 public String (byte[] ascii, int hibyte)
111 init(ascii, hibyte, 0, ascii.length);
115 public String (byte[] ascii, int hibyte, int offset, int count)
117 init(ascii, hibyte, offset, count);
120 public String toString ()
125 public native boolean equals (Object anObject);
127 public native int hashCode ();
134 public native char charAt (int index);
136 public native void getChars (int srcBegin, int srcEnd,
137 char[] dst, int dstBegin);
139 public byte[] getBytes ()
143 return getBytes (System.getProperty("file.encoding", "8859_1"));
145 catch (UnsupportedEncodingException x)
147 // This probably shouldn't happen, but could if file.encoding
148 // is somehow changed to a value we don't understand.
151 return getBytes ("8859_1");
153 catch (UnsupportedEncodingException x2)
155 // This really shouldn't happen, because the 8859_1
156 // encoding should always be available.
157 throw new InternalError ("couldn't find 8859_1 encoder");
162 public native byte[] getBytes (String enc)
163 throws UnsupportedEncodingException;
166 public native void getBytes (int srcBegin, int srcEnd,
167 byte[] dst, int dstBegin);
169 public native char[] toCharArray ();
171 public native boolean equalsIgnoreCase (String anotherString);
173 public native int compareTo (String anotherString);
175 public native boolean regionMatches (int toffset,
176 String other, int ooffset, int len);
178 public native boolean regionMatches (boolean ignoreCase, int toffset,
179 String other, int ooffset, int len);
181 public boolean startsWith (String prefix)
183 return startsWith (prefix, 0);
186 public native boolean startsWith (String prefix, int toffset);
188 public boolean endsWith (String suffix)
190 return regionMatches (this.count - suffix.count, suffix, 0, suffix.count);
193 // No such method specified in the doc, including JDK 1.2.
194 // public boolean endsWith (String suffix, int toffset)
196 // return regionMatches (toffset, suffix, 0, suffix.count);
199 // The Language Specification, and the JDK 1.2 API docs say that
200 // index and lastIndex take an int, while the Class Libraries
201 // say they take a char. The former wins ...
203 public int indexOf (int ch)
205 return indexOf (ch, 0);
208 public native int indexOf (int ch, int fromIndex);
210 public int indexOf (String str)
212 return indexOf (str, 0);
215 public native int indexOf (String str, int fromIndex);
217 public int lastIndexOf (int ch)
219 return lastIndexOf (ch, count - 1);
222 public native int lastIndexOf (int ch, int fromIndex);
224 public int lastIndexOf (String str)
226 return lastIndexOf (str, count - str.count);
229 public int lastIndexOf (String str, int fromIndex)
231 if (fromIndex >= count)
232 fromIndex = count - str.count;
237 if (startsWith(str, fromIndex))
242 public String substring (int beginIndex)
244 return substring (beginIndex, count);
247 public native String substring (int beginIndex, int endIndex);
249 public native String concat (String str);
251 public native String replace (char oldChar, char newChar);
253 public native String toLowerCase ();
255 public native String toUpperCase ();
257 public native String trim ();
259 public static String valueOf (Object obj)
261 return obj == null ? "null" : obj.toString();
264 public static String valueOf (char[] data)
266 return valueOf (data, 0, data.length);
269 public static native String valueOf (char[] data, int offset, int count);
271 public static String valueOf (boolean b)
273 return b ? "true" : "false";
276 public static native String valueOf (char c);
278 public static String valueOf (int i)
280 return Integer.toString(i);
283 public static String valueOf (long l)
285 return Long.toString(l);
288 public static String valueOf (float f)
290 return Float.toString(f);
293 public static String valueOf (double d)
295 return Double.toString(d);
298 public native String intern ();
300 private native void init ();
301 private native void init (char[] chars, int offset, int count,
303 private native void init (byte[] chars, int hibyte, int offset, int count);
304 private native void init (byte[] chars, int offset, int count, String enc)
305 throws UnsupportedEncodingException;
306 private static native void unintern (Object obj);
307 private static native void rehash ();