📄 javalang.doc11.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javalang.doc10.html">Prev</a> | <a href="javalang.doc12.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="14460"></a>
<center><h1>20.12 The Class <code>java.lang.String</code></h1></center>
<a name="3996"></a>
An object of type <code>String</code>, once created, is immutable. It represents a fixed-length
sequence of characters. Compare this to the class <code>StringBuffer</code> <a href="javalang.doc12.html#14461">(§20.13)</a>, which
represents a modifiable, variable-length sequence of characters.
<p><a name="3997"></a>
The class <code>String</code> has methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, for creating a copy of a string with all characters translated to uppercase or to lowercase, and so on.<p>
<pre><a name="13978"></a>public final class <code><b>String</b></code> {
<a name="2914"></a> public <code><b>String</b></code>();
<a name="31197"></a> public <code><b>String</b></code>(String value)
<a name="2915"></a> throws NullPointerException;
<a name="31200"></a> public <code><b>String</b></code>(StringBuffer buffer)
<a name="2916"></a> throws NullPointerException;
<a name="31190"></a> public <code><b>String</b></code>(char[] value)
<a name="2917"></a> throws NullPointerException;
<a name="2918"></a> public <code><b>String</b></code>(char[] value, int offset, int count)
<a name="25471"></a> throws <code>NullPointerException</code>, IndexOutOfBoundsException;
<a name="2919"></a> public <code><b>String</b></code>(byte[] ascii, int hibyte)
<a name="31215"></a> throws NullPointerException;
<a name="2920"></a> public <code><b>String</b></code>(byte[] ascii, int hibyte,
<a name="25148"></a> int offset, int count)
<a name="25476"></a> throws <code>NullPointerException</code>, IndexOutOfBoundsException;
<a name="2921"></a> public String <code><b>toString</b></code>();
<a name="2922"></a> public boolean <code><b>equals</b></code>(Object anObject);
<a name="2923"></a> public int <code><b>hashCode</b></code>();
<a name="2924"></a> public int <code><b>length</b></code>();
<a name="2925"></a> public char <code><b>charAt</b></code>(int index);
<a name="2926"></a> public void <code><b>getChars</b></code>(int srcBegin, int srcEnd,
<a name="31148"></a> char dst[], int dstBegin)<br>
throws NullPointerException, IndexOutOfBoundsException;
<a name="2927"></a> public void <code><b>getBytes</b></code>(int srcBegin, int srcEnd,
<a name="31145"></a> byte dst[], int dstBegin)
<a name="31140"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="2928"></a> public char[] <code><b>toCharArray</b></code>();
<a name="2929"></a> public boolean <code><b>equalsIgnoreCase</b></code>(String anotherString);
<a name="2930"></a> public int <code><b>compareTo</b></code>(String anotherString)
<a name="31155"></a> throws NullPointerException;
<a name="31157"></a> public boolean <code><b>regionMatches</b></code>(int toffset, String other,<br>
int ooffset, int len)
<a name="2931"></a> throws NullPointerException;
<a name="31160"></a> public boolean <code><b>regionMatches</b></code>(boolean ignoreCase, int toffset,<br>
String other, int ooffset, int len)
<a name="2932"></a> throws NullPointerException;
<a name="31163"></a> public boolean <code><b>startsWith</b></code>(String prefix)
<a name="2933"></a> throws NullPointerException;
<a name="31166"></a> public boolean <code><b>startsWith</b></code>(String prefix, int toffset)
<a name="2934"></a> throws NullPointerException;
<a name="31169"></a> public boolean <code><b>endsWith</b></code>(String suffix)
<a name="2935"></a> throws NullPointerException;
<a name="2936"></a> public int <code><b>indexOf</b></code>(int ch);
<a name="2937"></a> public int <code><b>indexOf</b></code>(int ch, int fromIndex);
<a name="31172"></a> public int <code><b>indexOf</b></code>(String str)
<a name="2938"></a> throws NullPointerException;
<a name="31175"></a> public int <code><b>indexOf</b></code>(String str, int fromIndex)
<a name="2939"></a> throws NullPointerException;
<a name="2940"></a> public int <code><b>lastIndexOf</b></code>(int ch);
<a name="2941"></a> public int <code><b>lastIndexOf</b></code>(int ch, int fromIndex);
<a name="31178"></a> public int <code><b>lastIndexOf</b></code>(String str)
<a name="2942"></a> throws NullPointerException;
<a name="31181"></a> public int <code><b>lastIndexOf</b></code>(String str, int fromIndex)
<a name="2943"></a> throws NullPointerException;
<a name="2944"></a> public String <code><b>substring</b></code>(int beginIndex);
<a name="2945"></a> public String <code><b>substring</b></code>(int beginIndex, int endIndex);
<a name="31184"></a> public String <code><b>concat</b></code>(String str)
<a name="2946"></a> throws NullPointerException;
<a name="2947"></a> public String <code><b>replace</b></code>(char oldChar, char newChar);
<a name="2948"></a> public String <code><b>toLowerCase</b></code>();
<a name="2949"></a> public String <code><b>toUpperCase</b></code>();
<a name="2950"></a> public String <code><b>trim</b></code>();
<a name="2951"></a> public static String <code><b>valueOf</b></code>(Object obj);
<a name="31187"></a> public static String <code><b>valueOf</b></code>(char[] data)
<a name="2952"></a> throws NullPointerException;
<a name="2953"></a> public static String <code><b>valueOf</b></code>(char[] data,
<a name="25149"></a> int offset, int count)
<a name="25466"></a> throws <code>NullPointerException</code>, IndexOutOfBoundsException;
<a name="2956"></a> public static String <code><b>valueOf</b></code>(boolean b);
<a name="2957"></a> public static String <code><b>valueOf</b></code>(char c);
<a name="2958"></a> public static String <code><b>valueOf</b></code>(int i);
<a name="2959"></a> public static String <code><b>valueOf</b></code>(long l);
<a name="2960"></a> public static String <code><b>valueOf</b></code>(float f);
<a name="2961"></a> public static String <code><b>valueOf</b></code>(double d);
<a name="2962"></a> public String <code><b>intern</b></code>();
<a name="2974"></a>}
</pre><a name="13979"></a>
<p><font size=+1><strong>20.12.1 </strong> <code>public <code><b>String</b></code>()</code></font>
<p>
<a name="4006"></a>
This constructor initializes a newly created <code>String</code> object so that it represents an
empty character sequence.
<p><a name="13980"></a>
<p><font size=+1><strong>20.12.2 </strong> <code>public <code><b>String</b></code>(String value)</code></font>
<p>
<a name="31205"></a>
This constructor initializes a newly created <code>String</code> object so that it represents the
same sequence of characters as the argument; in other words, the newly created
string is a copy of the argument string.
<p><a name="31206"></a>
<p><font size=+1><strong>20.12.3 </strong> <code>public <code><b>String</b></code>(StringBuffer buffer)<br>throws NullPointerException</code></font>
<p>
<a name="31207"></a>
This constructor initializes a newly created <code>String</code> object so that it represents the
sequence of characters that is currently contained in the <code>StringBuffer</code> argument
<a href="javalang.doc12.html#14461">(§20.13)</a>. The contents of the string buffer are copied; subsequent modification of
the string buffer does not affect the newly created string.
<p><a name="31239"></a>
If <code>buffer</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="4024"></a>
<p><font size=+1><strong>20.12.4 </strong> <code>public <code><b>String</b></code>(char[] data)<br>throws NullPointerException</code></font>
<p>
<a name="4044"></a>
This constructor initializes a newly created <code>String</code> object so that it represents the
sequence of characters currently contained in the character array argument. The
contents of the character array are copied; subsequent modification of the character
array does not affect the newly created string.
<p><a name="31243"></a>
If <code>data</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="34330"></a>
<p><font size=+1><strong>20.12.5 </strong> <code>public <code><b>String</b></code>(char[] data, int offset, int count)<br>throws <code>NullPointerException</code>,      IndexOutOfBoundsException</code></font>
<p>
<a name="34331"></a>
This constructor initializes a newly created <code>String</code> object so that it represents the
sequence of characters currently contained in a subarray of the character array
argument. The <code>offset</code> argument is the index of the first character of the subarray
and the <code>count</code> argument specifies the length of the subarray. The contents of the
subarray are copied; subsequent modification of the character array does not affect
the newly created string.
<p><a name="25495"></a>
If <code>data</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="25496"></a>
If <code>offset</code> is negative, or <code>count</code> is negative, or <code>offset+count</code> is larger than <code>data.length</code>, then an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="4041"></a>
<p><font size=+1><strong>20.12.6 </strong> <code>public <code><b>String</b></code>(byte[] ascii, int hibyte)<br>throws NullPointerException</code></font>
<p>
<a name="4137"></a>
This constructor initializes a newly created <code>String</code> object so that it represents a
sequence of characters constructed from an array of 8-bit integer values. Each
character <code>c</code> in the result string is constructed from the corresponding element <code>b</code> of
the byte array in such a way that:
<p><pre><a name="4144"></a><i>c</i> == ((hibyte & 0xff) << 8) | (<i>b</i> & 0xff)
</pre><a name="31245"></a>
If <code>ascii</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="4151"></a>
<p><font size=+1><strong>20.12.7 </strong> <code>public <code><b>String</b></code>(byte[] ascii, int hibyte,<br>      int offset, int count) <br>throws<code> NullPointerException</code>,<br>      IndexOutOfBoundsException</code></font>
<p>
<a name="4174"></a>
This constructor initializes a newly created <code>String</code> object so that it represents the
sequence of characters constructed from a subarray of an array of 8-bit integer
values. The <code>offset</code> argument is the index of the first byte of the subarray and the
<code>count</code> argument specifies the length of the subarray. Each character <code>c</code> in the result
string is constructed from the corresponding element <code>b</code> of the byte subarray in
such a way that:
<p><pre><a name="4175"></a><i>c</i> == ((hibyte & 0xff) << 8) | (<i>b</i> & 0xff)
</pre><a name="25504"></a>
If <code>ascii</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.<p>
<a name="25505"></a>
If <code>offset</code> is negative, or <code>count</code> is negative, or <code>offset+count</code> is larger than <code>ascii.length</code>, then an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="4156"></a>
<p><font size=+1><strong>20.12.8 </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="4153"></a>
A reference to this object (which is, after all, already a <code>String</code>) is returned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -