字符串
原文: https://docs.oracle.com/javase/tutorial/java/data/strings.html
在 Java 编程中广泛使用的字符串是一系列字符。在 Java 编程语言中,字符串是对象。
Java 平台提供 String 类来创建和操作字符串。
创建字符串
创建字符串的最直接方法是编写:
String greeting = "Hello world!";
在这种情况下,“Hello world!”是*字符串文字 _ - 代码中的一系列字符,用双引号括起来。每当它在代码中遇到字符串文字时,编译器就会创建一个String对象及其值 - 在本例中为Hello world!。
与任何其他对象一样,您可以使用new关键字和构造器创建String对象。 String类有 13 个构造器,允许您使用不同的源提供字符串的初始值,例如字符数组:
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };String helloString = new String(helloArray);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:
String palindrome = "Dot saw I was Tod";int len = palindrome.length();
回文是一个对称的单词或句子 - 它拼写相同的向前和向后,忽略大小写和标点符号。这是一个简短而低效的程序来反转回文串。它调用String方法charAt(i),它返回字符串中的 i <sup>th</sup> 字符,从 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 charsfor (int i = 0; i < len; i++) {tempCharArray[i] =palindrome.charAt(i);}// reverse array of charsfor (int j = 0; j < len; j++) {charArray[j] =tempCharArray[len - 1 - j];}String reversePalindrome =new String(charArray);System.out.println(reversePalindrome);}}
运行该程序会产生以下输出:
doT saw I was toD
要完成字符串反转,程序必须将字符串转换为字符数组(第一个for循环),将数组反转为第二个数组(第二个for循环),然后转换回字符串。 String 类包含一个方法getChars(),用于将字符串或字符串的一部分转换为字符数组,以便我们可以替换上面程序中的第一个for循环同
palindrome.getChars(0, len, tempCharArray, 0);
连接字符串
String类包括一个连接两个字符串的方法:
string1.concat(string2);
这将返回一个新字符串,该字符串为 string1,并在结尾处添加了 string2。
您还可以将concat()方法与字符串文字一起使用,如下所示:
"My name is ".concat("Rumplestiltskin");
字符串通常与+运算符连接,如
"Hello," + " world" + "!"
结果
"Hello, world!"
+运算符广泛用于print语句。例如:
String string1 = "saw I was ";System.out.println("Dot " + string1 + "Tod");
打印
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:
String quote ="Now is the time for all good " +"men to come to the aid of their country.";
使用+连接运算符在行之间断开字符串在print语句中再次非常常见。
创建格式字符串
您已经看到使用printf()和format()方法打印带格式化数字的输出。 String类有一个等效的类方法format(),它返回String对象而不是PrintStream对象。
使用String's static format()方法可以创建可以重复使用的格式化字符串,而不是一次性打印语句。例如,而不是
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);
你可以写
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);
