在本教程中,我们将在示例的帮助下了解 Java String,如何创建 Java String及其各种方法。
在 Java 中,字符串是字符序列。 例如,"hello"是包含以下字符序列的字符串:'h','e','l','l'和'o'。
与其他编程语言不同,Java 中的字符串不是原始类型(例如int,char等)。 相反,所有字符串都是名为String的预定义类的对象。 例如,
// create a stringString type = "java programming";
在这里,我们创建了一个名为type的字符串。 在这里,我们使用"java programming初始化了字符串。 在 Java 中,我们使用双引号表示字符串。
该字符串是String类的实例。
注意:所有字符串变量都是String类的实例。
Java 字符串方法
Java String提供了各种方法,使我们可以执行不同的字符串操作。 以下是一些常用的字符串方法。
| 方法 | 描述 |
|---|---|
concat() |
将两个字符串连接在一起 |
equals() |
比较两个字符串的值 |
charAt() |
返回存在于指定位置的字符 |
getBytes() |
将字符串转换为字节数组 |
indexOf() |
返回字符串中指定字符的位置 |
length() |
返回指定字符串的大小 |
replace() |
将指定的旧字符替换为指定的新字符 |
substring() |
返回字符串的子字符串 |
split() |
将字符串分成字符串数组 |
toLowerCase() |
将字符串转换为小写 |
toUpperCase() |
将字符串转换为大写 |
valueOf() |
返回指定数据的字符串表示形式 |
让我们举几个例子。
示例 1:查找字符串的长度
class Main {public static void main(String[] args) {// create a stringString greet = "Hello! World";System.out.println("The string is: " + greet);//checks the string lengthSystem.out.println("The length of the string: " + greet.length());}}
输出
The string is: Hello! WorldThe length of the string: 12
在上面的示例中,我们创建了一个名为greet的字符串。 这里我们使用了length()方法来获取字符串的大小。
示例 2:使用concat()连接两个字符串
class Main {public static void main(String[] args) {// create stringString greet = "Hello! ";System.out.println("First String: " + greet);String name = "World";System.out.println("Second String: " + name);// join two stringsString joinedString = greet.concat(name);System.out.println("Joined String: " + joinedString);}}
输出:
First String: Hello!Second String: WorldJoined String: Hello! World
在上面的示例中,我们创建了 2 个名为greet和name的字符串。
在这里,我们使用了concat()方法来连接字符串。 因此,我们得到一个名为joinedString的新字符串。
在 Java 中,我们还可以使用+运算符连接两个字符串。
示例 3:使用+运算符连接字符串
class Main {public static void main(String[] args) {// create stringString greet = "Hello! ";System.out.println("First String: " + greet);String name = "World";System.out.println("Second String: " + name);// join two stringsString joinedString = greet + name;System.out.println("Joined String: " + joinedString);}}
输出:
First String: Hello!Second String: WorldJoined String: Hello! World
在这里,我们使用了+运算符来连接两个字符串。
示例 4:比较两个字符串
class Main {public static void main(String[] args) {// create stringsString first = "java programming";String second = "java programming";String third = "python programming";// compare first and second stringsboolean result1 = first.equals(second);System.out.println("Strings first and second are equal: " + result1);//compare first and third stringsboolean result2 = first.equals(third);System.out.println("Strings first and third are equal: " + result2);}}
输出:
Strings first and second are equal: trueStrings first and third are equal: false
在上面的示例中,我们使用equals()方法比较两个字符串的值。
如果两个字符串相同,则该方法返回true,否则返回false。
注意:我们也可以使用==运算符和compareTo()方法在 2 个字符串之间进行比较。
示例 5:从字符串中获取字符
class Main {public static void main(String[] args) {// create string using the string literalString greet = "Hello! World";System.out.println("The string is: " + greet);// returns the character at 3System.out.println("The character at 3: " + greet.charAt(3));// returns the character at 7System.out.println("The character at 7: " + greet.charAt(7));}}
输出:
The string is: Hello! WorldThe character at 3: lThe character at 7: W
在上面的示例中,我们使用charAt()方法从指定位置访问字符。
示例 6:String其他方法
class Main {public static void main(String[] args) {// create string using the new keywordString example = new String("Hello! World");// returns the substring WorldSystem.out.println("Using the subString(): " + example.substring(7));// converts the string to lowercaseSystem.out.println("Using the toLowerCase(): " + example.toLowerCase());// converts the string to uppercaseSystem.out.println("Using the toUpperCase(): " + example.toUpperCase());// replaces the character '!' with 'o'System.out.println("Using the replace(): " + example.replace('!', 'o'));}}
输出:
Using the subString(): WorldUsing the toLowerCase(): hello! worldUsing the toUpperCase(): HELLO! WORLDUsing the replace(): Helloo World
在上面的示例中,我们使用new关键字创建了一个名为example的字符串。
这里,
substring()方法返回字符串WorldtoLowerCase()方法将字符串转换为小写toUpperCase()方法将字符串转换为大写replace()方法替换字符'!'与'o'。
字符串中的转义字符
Java 中的字符串由双引号表示。 例如,
// create a stringString example = "This is a string";
现在,如果我们想在字符串中包含双引号。 例如,
// include double quoteString example = "This is the "String" class";
这将导致错误。 这是因为双引号用于表示字符串。 因此,编译器会将"this"视为字符串。
为了解决此问题,在 Java 中使用了转义字符\。 现在我们可以在字符串中包含双引号,如下所示:
// use the escape characterString example = "This is the \"String\" class.";
转义字符告诉编译器转义双引号并读取全文。
Java 字符串是不可变的
在 Java 中,创建字符串意味着创建String类的对象。 创建字符串时,无法在 Java 中更改该字符串。 这就是为什么在 Java 中将字符串称为不可变的原因。
为了更深入地理解它,让我们考虑一个示例:
// create a stringString example = "Hello!";
在这里,我们创建了一个字符串对象"Hello!"。 创建之后,我们将无法更改它。
现在假设我们要更改字符串。
// adds another string to the stringexample = example.concat(" World");
在这里,我们尝试将新的字符串添加到先前的字符串。
由于字符串是不变的,因此应引起错误。 但这很好。
现在看来我们可以更改字符串了。 但是,事实并非如此。 让我们看看这里实际发生了什么。
我们有一个字符串"Hello!",由名为example的变量引用。 现在,在执行上面的代码时,
- JVM 使用字符串
"Hello!" - 附加字符串
"World" - 这将创建一个新字符串
"Hello!World" - 变量
example现在引用新字符串 - 上一个字符串
"Hello!"保持不变
注意:每次创建新字符串并由变量引用它时。
使用new关键字创建字符串
到目前为止,我们已经在 Java 中创建了类似于基本类型的字符串。 但是,由于 Java 中的字符串是对象,因此我们也可以使用new关键字进行创建。 例如,
// create a string using the new keywordString name = new String("java string");
在上面的示例中,我们将new关键字与构造器String()一起使用来创建字符串。
String类提供了各种其他构造器来创建字符串。 要了解所有这些构造器,请访问 Java String(Java 官方文档)。
现在,让我们看一下创建字符串的过程与上一个过程有何不同。
使用字符串字面值和new关键字之间的区别
现在我们知道如何使用字符串字面值和new关键字创建字符串,让我们看看它们之间的主要区别是什么。
在 Java 中,JVM 维护一个字符串池以将其所有字符串存储在内存中。 字符串池有助于重用字符串。
使用字符串字面值创建字符串时,将直接提供字符串的值。 因此,编译器首先检查字符串池以查看字符串是否已经存在。
- 如果字符串已经存在,则不会创建新字符串。 而是,新引用指向现有的字符串。
- 如果字符串不存在,则创建新字符串。
但是,在使用new关键字创建字符串时,不会直接提供字符串的值。 因此,新字符串始终被创建。
