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发行说明。
Strings, which are widely used in Java programming, are a sequence of characters.字符串是一个字符序列,在Java编程中被广泛使用。In the Java programming language, strings are objects.在Java编程语言中,字符串是对象。
The Java platform provides the Java平台提供了String
class to create and manipulate strings.String
类来创建和操作字符串。
The most direct way to create a string is to write:创建字符串最直接的方法是写入:
String greeting = "Hello world!";
In this case, "Hello world!" is a string literala series of characters in your code that is enclosed in double quotes.在本例中,“你好,世界!”是字符串文本代码中用双引号括起来的一系列字符。Whenever it encounters a string literal in your code, the compiler creates a 每当它在代码中遇到字符串文字时,编译器就会创建一个String
object with its valuein this case, Hello world!
.String
对象,在这种情况下,其值为Hello world!
。
As with any other object, you can create 与任何其他对象一样,可以使用String
objects by using the new
keyword and a constructor.new
关键字和构造函数创建String
对象。The String
class has thirteen constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:String
类有13个构造函数,允许您使用不同的源(例如字符数组)提供字符串的初始值:
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' }; String helloString = new String(helloArray); System.out.println(helloString);
The last line of this code snippet displays 此代码段的最后一行显示hello
.hello
。
String
class is immutable, so that once it is created a String
object cannot be changed.String
类是不可变的,因此一旦创建了String
对象,就不能对其进行更改。String
class has a number of methods, some of which will be discussed below, that appear to modify strings.String
类有许多方法,下面将讨论其中一些方法,它们似乎可以修改字符串。Methods used to obtain information about an object are known as accessor methods.用于获取对象信息的方法称为访问器方法。One accessor method that you can use with strings is the 可以用于字符串的一个访问器方法是length()
method, which returns the number of characters contained in the string object.length()
方法,它返回字符串对象中包含的字符数。After the following two lines of code have been executed, 执行以下两行代码后,len
equals 17:len
等于17:
String palindrome = "Dot saw I was Tod"; int len = palindrome.length();
A palindrome is a word or sentence that is symmetricit is spelled the same forward and backward, ignoring case and punctuation.回文是一个对称的单词或句子它前后拼写相同,忽略大小写和标点符号。Here is a short and inefficient program to reverse a palindrome string.下面是一个简短而低效的程序来反转回文字符串。It invokes the 它调用String
method charAt(i)
, which returns the ith character in the string, counting from 0.String
方法charAt(i)
,该方法返回字符串中的第i
个字符,从0
开始计数。
public class StringDemo { public static void main(String[] args) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); char[] tempCharArray = new char[len]; char[] charArray = new char[len]; // put original string in an // array of chars for (int i = 0; i < len; i++) { tempCharArray[i] = palindrome.charAt(i); } // reverse array of chars for (int j = 0; j < len; j++) { charArray[j] = tempCharArray[len - 1 - j]; } String reversePalindrome = new String(charArray); System.out.println(reversePalindrome); } }
Running the program produces this output:运行该程序会产生以下输出:
doT saw I was toD
To accomplish the string reversal, the program had to convert the string to an array of characters (first 为了完成字符串反转,程序必须将字符串转换为字符数组(第一个for
loop), reverse the array into a second array (second for
loop), and then convert back to a string.for
循环),将数组反转为第二个数组(第二个for
循环),然后再转换回字符串。The String
class includes a method, getChars()
, to convert a string, or a portion of a string, into an array of characters so we could replace the first for
loop in the program above withString
类包括一个getChars()
方法,用于将字符串或字符串的一部分转换为字符数组,所以我们可以将上面程序中的第一个for
循环替换为
palindrome.getChars(0, len, tempCharArray, 0);
The String
class includes a method for concatenating two strings:String
类包括一个用于连接两个字符串的方法:
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end.这将返回一个新字符串,该字符串为string1
,并在末尾添加string2
。
You can also use the 您还可以将concat()
method with string literals, as in:concat()
方法用于字符串文本,如中所示:
"My name is ".concat("Rumplestiltskin");
Strings are more commonly concatenated with the 字符串通常使用+
operator, as in+
运算符连接,如下所示
"Hello," + " world" + "!"
which results in导致
"Hello, world!"
The +
operator is widely used in print
statements.+
运算符广泛用于打印语句。For example:例如:
String string1 = "saw I was "; System.out.println("Dot " + string1 + "Tod");
which prints打印了
Dot saw I was Tod
Such a concatenation can be a mixture of any objects.这种连接可以是任何对象的混合。For each object that is not a 对于每个不是String
, its toString()
method is called to convert it to a String
.String
的对象,调用其toString()
方法将其转换为字符串。
+
concatenation operator at the end of each line in a multi-line string.+
串联运算符。String quote = "Now is the time for all good " + "men to come to the aid of their country.";
Breaking strings between lines using the 在+
concatenation operator is, once again, very common in print
statements.print
语句中,使用+
串联运算符在行之间断开字符串再次非常常见。
You have seen the use of the 您已经看到了使用printf()
and format()
methods to print output with formatted numbers.printf()
和format()
方法打印带格式数字的输出。The String
class has an equivalent class method, format()
, that returns a String
object rather than a PrintStream
object.String
类有一个等价的类方法format()
,它返回一个String
对象而不是PrintStream
对象。
Using 使用String
's static format()
method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement.String
的静态format()
方法可以创建可重用的格式化字符串,而不是一次性打印语句。For example, instead of例如,代替
System.out.printf("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar);
you can write你可以写
String fs; fs = String.format("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + " and the string is %s", floatVar, intVar, stringVar); System.out.println(fs);