Java 字符串

Java 字符串

字符串用于存储文本。

一个 String 字符串变量包含由双引号包围的字符集合:

实例

创建一个 String 字符串类型的变量,并为其赋值:

  1. public class Main {
  2. public static void main(String[] args) {
  3. String greeting = "Hello";
  4. System.out.println(greeting);
  5. }
  6. }

字符串长度

Java 中的字符串实际上是一个对象,其中包含可以对字符串执行某些操作的方法。例如,字符串的长度可以通过 length() 方法找到:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4. System.out.println("The length of the txt string is: " + txt.length());
  5. }
  6. }

更多字符串方法

有许多字符串方法可用,例如 toUpperCase()toLowerCase():

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "Hello World";
  4. System.out.println(txt.toUpperCase());
  5. System.out.println(txt.toLowerCase());
  6. }
  7. }

在字符串中查找字符

indexOf() 方法返回字符串(包括空格)中指定文本第一次出现的 索引(位置):

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "Please locate where 'locate' occurs!";
  4. System.out.println(txt.indexOf("locate"));
  5. }
  6. }

Java 从 0 开始计算位置。

0 是字符串中的第一个位置,1 是第二个位置,2 是第三个位置…

串联字符串

+ 运算符可用于字符串之间的组合。这叫做串联 concatenation:

实例
  1. public class Main {
  2. public static void main(String args[]) {
  3. String firstName = "John";
  4. String lastName = "Doe";
  5. System.out.println(firstName + " " + lastName);
  6. }
  7. }

请注意,我们添加了一个空文本(" "),以便在打印时在firstName和lastName之间创建一个空格。

还可以使用 concat() 方法连接两个字符串:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String firstName = "John ";
  4. String lastName = "Doe";
  5. System.out.println(firstName.concat(lastName));
  6. }
  7. }

特殊字符

因为字符串必须写在引号内,所以 Java 解析这个字符串会产生错误:

  1. String txt = "We are the so-called "Vikings" from the north.";

避免此问题的解决方案是使用 反斜杠转义字符

反斜杠(\)转义字符将特殊字符转换为字符串:

转义字符结果描述
\''单引号
\""双引号
\\反斜杠

在字符串中以 \” 方式插入双引号:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "We are the so-called \"Vikings\" from the north.";
  4. System.out.println(txt);
  5. }
  6. }

在字符串中以 \’ 方式插入单引号:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "It\'s alright.";
  4. System.out.println(txt);
  5. }
  6. }

在字符串中以 \ 方式插入反斜杠:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String txt = "The character \\ is called backslash.";
  4. System.out.println(txt);
  5. }
  6. }

其他 6 个在 Java 中有效的转义字符:

代码结果试一试
\n新的一行试一试 »
\r回车试一试 »
\tTab试一试 »
\b回退试一试 »
\f表单提交

添加数字和字符串

警告! Java 对加法和连接都会使用 + 运算符

数字是加,字符串是连接。

如果你加上两个数字,结果就是一个数字:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. int x = 10;
  4. int y = 20;
  5. int z = x + y;
  6. System.out.println(z);
  7. }
  8. }

如果添加两个字符串,结果将是字符串串联:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String x = "10";
  4. String y = "20";
  5. String z = x + y;
  6. System.out.println(z);
  7. }
  8. }

如果添加一个数字和一个字符串,结果将是字符串串联:

实例
  1. public class Main {
  2. public static void main(String[] args) {
  3. String x = "10";
  4. int y = 20;
  5. String z = x + y;
  6. System.out.println(z);
  7. }
  8. }

完整字符串参考

有关字符串方法的完整参考,请访问本站的 Java 字符串方法参考

参考包含所有字符串方法的描述和示例。