The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The String
class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks.String
类有许多方法用于检查字符串的内容、查找字符串中的字符或子字符串、更改大小写以及其他任务。
You can get the character at a particular index within a string by invoking the 通过调用charAt()
accessor method.charAt()
访问器方法,可以获取字符串中特定索引处的字符。The index of the first character is 0, while the index of the last character is 第一个字符的索引为0,而最后一个字符的索引为length()-1
.length()-1
。For example, the following code gets the character at index 9 in a string例如,以下代码获取字符串中索引9
处的字符:
String anotherPalindrome = "Niagara. O roar again!"; char aChar = anotherPalindrome.charAt(9);
Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure:索引从0开始,因此索引9处的字符为“O”,如下图所示:
If you want to get more than one consecutive character from a string, you can use the 如果要从字符串中获取多个连续字符,可以使用substring
method.substring
方法。The substring
method has two versions, as shown in the following table:substring
方法有两个版本,如下表所示:
String substring(int beginIndex, int endIndex) |
beginIndex and extends to the character at index endIndex - 1 .beginIndex 开始,并延伸到索引endIndex-1 处的字符。 |
String substring(int beginIndex) |
The following code gets from the Niagara palindrome the substring that extends from index 11 up to, but not including, index 15, which is the word "roar":下面的代码来自Niagara回文,它是从索引11扩展到但不包括索引15的子字符串,索引15是单词“roar”:
String anotherPalindrome = "Niagara. O roar again!"; String roar = anotherPalindrome.substring(11, 15);
Here are several other 以下是用于操作字符串的其他几种String
methods for manipulating strings:String
方法:
String[] split(String regex) String[] split(String regex, int limit) |
|
CharSequence subSequence(int beginIndex, int endIndex) |
beginIndex index up until endIndex - 1.beginIndex 索引到endIndex-1 构造的新字符序列。 |
String trim() |
|
String toLowerCase() |
Here are some other 下面是一些用于查找字符串中的字符或子字符串的其他String
methods for finding characters or substrings within a string.String
方法。The String
class provides accessor methods that return the position within the string of a specific character or substring: indexOf()
and lastIndexOf()
.String
类提供了访问器方法,用于返回特定字符或子字符串在字符串中的位置:indexOf()
和lastIndexOf()
。The indexOf()
methods search forward from the beginning of the string, and the lastIndexOf()
methods search backward from the end of the string.indexOf()
方法从字符串的开头向前搜索,lastIndexOf()
方法从字符串的结尾向后搜索。If a character or substring is not found, 如果未找到字符或子字符串,indexOf()
and lastIndexOf()
return -1.indexOf()
和lastIndexOf()
将返回-1
。
The String
class also provides a search method, contains
, that returns true if the string contains a particular character sequence.String
类还提供了一个搜索方法contains
,如果字符串包含特定的字符序列,该方法将返回true
。Use this method when you only need to know that the string contains a character sequence, but the precise location isn't important.当您只需要知道字符串包含字符序列,但精确位置并不重要时,请使用此方法。
The following table describes the various string search methods.下表介绍了各种字符串搜索方法。
int indexOf(int ch) |
|
int indexOf(int ch, int fromIndex) |
|
int indexOf(String str) |
|
int indexOf(String str, int fromIndex) |
|
boolean contains(CharSequence s) |
true 。 |
CharSequence
is an interface that is implemented by the String
class.CharSequence
是由String
类实现的接口。contains()
method.contains()
方法的参数。
The String
class has very few methods for inserting characters or substrings into a string.String
类很少有将字符或子字符串插入字符串的方法。In general, they are not needed: You can create a new string by concatenation of substrings you have removed from a string with the substring that you want to insert.通常,它们是不需要的:您可以通过将从字符串中删除的子字符串与要插入的子字符串连接起来来创建新字符串。
The 但是,String
class does have four methods for replacing found characters or substrings, however.String
类有四种方法来替换找到的字符或子字符串。They are:他们是:
String replace(char oldChar, char newChar) |
|
String replace(CharSequence target, CharSequence replacement) |
|
String replaceAll(String regex, String replacement) |
|
String replaceFirst(String regex, String replacement) |
The following class, 下面的类Filename
, illustrates the use of lastIndexOf()
and substring()
to isolate different parts of a file name.Filename
说明了如何使用lastIndexOf()
和substring()
来隔离文件名的不同部分。
Filename
class don't do any error checking and assume that their argument contains a full directory path and a filename with an extension.Filename
类中的方法不进行任何错误检查,并假定其参数包含完整的目录路径和带有扩展名的文件名。public class Filename { private String fullPath; private char pathSeparator, extensionSeparator; public Filename(String str, char sep, char ext) { fullPath = str; pathSeparator = sep; extensionSeparator = ext; } public String extension() { int dot = fullPath.lastIndexOf(extensionSeparator); return fullPath.substring(dot + 1); } // gets filename without extension public String filename() { int dot = fullPath.lastIndexOf(extensionSeparator); int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(sep + 1, dot); } public String path() { int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(0, sep); } }
Here is a program, 下面是一个名为FilenameDemo
, that constructs a Filename
object and calls all of its methods:FilenameDemo
的程序,它构造一个Filename
对象并调用其所有方法:
public class FilenameDemo { public static void main(String[] args) { final String FPATH = "/home/user/index.html"; Filename myHomePage = new Filename(FPATH, '/', '.'); System.out.println("Extension = " + myHomePage.extension()); System.out.println("Filename = " + myHomePage.filename()); System.out.println("Path = " + myHomePage.path()); } }
And here's the output from the program:下面是程序的输出:
Extension = html Filename = index Path = /home/user
As shown in the following figure, our 如下图所示,我们的extension
method uses lastIndexOf
to locate the last occurrence of the period (.) in the file name.extension
方法使用lastIndexOf
来定位文件名中句点(.
)的最后一次出现。Then 然后,substring
uses the return value of lastIndexOf
to extract the file name extensionsubstring
使用lastIndexOf
的返回值来提取文件扩展名 that is, the substring from the period to the end of the string.也就是说,从句点到字符串末尾的子字符串。This code assumes that the file name has a period in it; if the file name does not have a period, 此代码假定文件名中有句点;如果文件名没有句点,lastIndexOf
returns -1, and the substring method throws a StringIndexOutOfBoundsException
.lastIndexOf
返回-1
,substring
方法抛出StringIndexOutOfBoundsException
。
Also, notice that the 另外,请注意extension
method uses dot + 1
as the argument to substring
.extension
方法使用dot+1
作为substring
的参数。If the period character (.) is the last character of the string, 如果句点字符(dot + 1
is equal to the length of the string, which is one larger than the largest index into the string (because indices start at 0)..
)是字符串的最后一个字符,则dot+1
等于字符串的长度,该长度比字符串中的最大索引大1(因为索引从0
开始)。This is a legal argument to 这是substring
because that method accepts an index equal to, but not greater than, the length of the string and interprets it to mean "the end of the string."substring
的合法参数,因为该方法接受等于但不大于字符串长度的索引,并将其解释为“字符串的结尾”。