字符串

原文: https://docs.oracle.com/javase/tutorial/java/data/strings.html

在 Java 编程中广泛使用的字符串是一系列字符。在 Java 编程语言中,字符串是对象。

Java 平台提供 String 类来创建和操作字符串。

创建字符串

创建字符串的最直接方法是编写:

  1. String greeting = "Hello world!";

在这种情况下,“Hello world!”是*字符串文字 _ - 代码中的一系列字符,用双引号括起来。每当它在代码中遇到字符串文字时,编译器就会创建一个String对象及其值 - 在本例中为Hello world!

与任何其他对象一样,您可以使用new关键字和构造器创建String对象。 String类有 13 个构造器,允许您使用不同的源提供字符串的初始值,例如字符数组:

  1. char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
  2. String helloString = new String(helloArray);
  3. System.out.println(helloString);

此代码段的最后一行显示hello


Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.


字符串长度

用于获取对象信息的方法称为存取方法。可以与字符串一起使用的一种访问器方法是length()方法,它返回字符串对象中包含的字符数。执行以下两行代码后,len等于 17:

  1. String palindrome = "Dot saw I was Tod";
  2. int len = palindrome.length();

回文是一个对称的单词或句子 - 它拼写相同的向前和向后,忽略大小写和标点符号。这是一个简短而低效的程序来反转回文串。它调用String方法charAt(i),它返回字符串中的 i <sup>th</sup> 字符,从 0 开始计数。

  1. public class StringDemo {
  2. public static void main(String[] args) {
  3. String palindrome = "Dot saw I was Tod";
  4. int len = palindrome.length();
  5. char[] tempCharArray = new char[len];
  6. char[] charArray = new char[len];
  7. // put original string in an
  8. // array of chars
  9. for (int i = 0; i < len; i++) {
  10. tempCharArray[i] =
  11. palindrome.charAt(i);
  12. }
  13. // reverse array of chars
  14. for (int j = 0; j < len; j++) {
  15. charArray[j] =
  16. tempCharArray[len - 1 - j];
  17. }
  18. String reversePalindrome =
  19. new String(charArray);
  20. System.out.println(reversePalindrome);
  21. }
  22. }

运行该程序会产生以下输出:

  1. doT saw I was toD

要完成字符串反转,程序必须将字符串转换为字符数组(第一个for循环),将数组反转为第二个数组(第二个for循环),然后转换回字符串。 String 类包含一个方法getChars(),用于将字符串或字符串的一部分转换为字符数组,以便我们可以替换上面程序中的第一个for循环同

  1. palindrome.getChars(0, len, tempCharArray, 0);

连接字符串

String类包括一个连接两个字符串的方法:

  1. string1.concat(string2);

这将返回一个新字符串,该字符串为 string1,并在结尾处添加了 string2。

您还可以将concat()方法与字符串文字一起使用,如下所示:

  1. "My name is ".concat("Rumplestiltskin");

字符串通常与+运算符连接,如

  1. "Hello," + " world" + "!"

结果

  1. "Hello, world!"

+运算符广泛用于print语句。例如:

  1. String string1 = "saw I was ";
  2. System.out.println("Dot " + string1 + "Tod");

打印

  1. Dot saw I was Tod

这种连接可以是任何对象的混合。对于不是String的每个对象,调用其toString()方法将其转换为String


Note: The Java programming language does not permit literal strings to span lines in source files, so you must use the + concatenation operator at the end of each line in a multi-line string. For example:

  1. String quote =
  2. "Now is the time for all good " +
  3. "men to come to the aid of their country.";

使用+连接运算符在行之间断开字符串在print语句中再次非常常见。


创建格式字符串

您已经看到使用printf()format()方法打印带格式化数字的输出。 String类有一个等效的类方法format(),它返回String对象而不是PrintStream对象。

使用String's static format()方法可以创建可以重复使用的格式化字符串,而不是一次性打印语句。例如,而不是

  1. System.out.printf("The value of the float " +
  2. "variable is %f, while " +
  3. "the value of the " +
  4. "integer variable is %d, " +
  5. "and the string is %s",
  6. floatVar, intVar, stringVar);

你可以写

  1. String fs;
  2. fs = String.format("The value of the float " +
  3. "variable is %f, while " +
  4. "the value of the " +
  5. "integer variable is %d, " +
  6. " and the string is %s",
  7. floatVar, intVar, stringVar);
  8. System.out.println(fs);