在 Java 中通过索引获取字符串中的字符
本教程介绍了如何在 Java 中通过索引获取 String
字符,并列出了一些示例代码来理解该主题。
字符串用于存储字符序列。就像数组一样,字符串也遵循从零开始的索引。这意味着字符串的第一个字符被分配了一个索引值为零。我们可以使用它们的索引从字符串中获取单个字符。在本教程中,我们将学习如何做到这一点。
Java 中使用 charAt()
方法获取字符串字符
charAt()
方法将索引值作为参数并返回字符串中该索引处的字符。它是从字符串中获取字符的最简单方法,是一个 String
类方法。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char characterAtIdx1 = s.charAt(1);//character e
char characterAtIdx4 = s.charAt(4);//whitespace
char characterAtIdx5 = s.charAt(5);//character s
System.out.println("The string is: " + s);
System.out.println("Character at Index 1: " + characterAtIdx1);
System.out.println("Character at Index 4: " + characterAtIdx4);
System.out.println("Character at Index 5: " + characterAtIdx5);
}
}
输出:
The string is: test string
Character at Index 1: e
Character at Index 4:
Character at Index 5: s
正如我们在上面的代码中看到的,这个方法的返回类型是 char。我们可以使用 Character
类的 toString()
方法将此字符类型转换为字符串,甚至可以使用 length()
方法获取此字符串的长度。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char characterAtIdx6 = s.charAt(6);//character t
String stringAtIdx6 = Character.toString(characterAtIdx6);//char to string
System.out.println("Length: " + stringAtIdx6.length());
System.out.println("String: " + stringAtIdx6);
}
}
输出:
Length: 1
String: t
使用 String.valueOf()
方法将字符转换为字符串
从字符串中获取字符后,如果需要,我们可以使用 String.valueOf()
方法将字符转换为字符。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char characterAtIdx6 = s.charAt(6);//character t
String stringAtIdx6 = String.valueOf(characterAtIdx6);//char to string
System.out.println("Length: " + stringAtIdx6.length());
System.out.println("String: " + stringAtIdx6);
}
}
输出:
Length: 1
String: t
我们还可以将一个空字符串连接到字符上以将其转换为字符串。中间没有任何内容的引号表示空字符串。这是在 Java 中获取 String
对象的最简单和隐式的方法之一。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char characterAtIdx6 = s.charAt(6);//character t
String stringAtIdx6 = "" + characterAtIdx6;//char to string
System.out.println("Length: " + stringAtIdx6.length());
System.out.println("String: " + stringAtIdx6);
}
}
输出:
Length: 1
String: t
Java 中从字符串获取字符数组
我们可以借助方括号通过索引获取数组元素。如果我们将字符串转换为字符类型数组,我们可以从中获取任何字符。
Java 提供了一个方便的 toCharArray()
方法,它返回一个字符数组。我们可以在字符串上使用这个方法并为它获取一个字符数组。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char[] charArr = s.toCharArray();
System.out.println("The string is: " + s);
System.out.println("Character at Index 1: " + charArr[1]);
System.out.println("Character at Index 4: " + charArr[4]);
System.out.println("Character at Index 5: " + charArr[5]);
}
}
输出:
The string is: test string
Character at Index 1: e
Character at Index 4:
Character at Index 5: s
我们可以使用上一节中描述的方法将字符转换为字符串。
public class Demo
{
public static void main(String[] args)
{
String s = "test string";
char[] charArr = s.toCharArray();
char characterAtIdx6 = charArr[6];//character t
String str1 = Character.toString(characterAtIdx6);//char to string
String str2 = String.valueOf(characterAtIdx6);//char to string
String str3 = "" + characterAtIdx6;//char to string
System.out.println("Using toString()");
System.out.println("Length: " + str1.length() + " String: " + str1);
System.out.println("\nUsing valueOf()");
System.out.println("Length: " + str2.length() + " String: " + str2);
System.out.println("\nUsing empty string concatenation");
System.out.println("Length: " + str3.length() + " String: " + str3);
}
}
输出:
Using toString()
Length: 1 String: t
Using valueOf()
Length: 1 String: t
Using empty string concatenation
Length: 1 String: t
获取 Unicode 补充多语言平面 (SMP) 的字符
在极少数情况下,我们的字符串可能包含基本 Unicode 多语言平面中不存在的字符。一个例子可能是一串表情符号或表情符号。这些字符是 Unicode 补充多语言平面 (SMP) 的一部分。
上述方法不适用于这些字符。我们可以使用 codePointAt()
方法从字符串中获取此类字符。此方法返回一个整数,表示该索引处字符的 Unicode 值。确保你安装了适当的字体来查看这些字符。
public class Demo
{
public static void main(String[] args)
{
String s = "😀 the grinning emoji";
for(int i=0; i<s.length();)
{
int codePoint = s.codePointAt(i);
char[] charArr = Character.toChars(codePoint);//More than one characters may be required to represent an SMP char
System.out.print(new String(charArr) + " ");
i = i + Character.charCount(codePoint);//Increase i according to the number of chars required for that SMP char
}
}
}
输出:
😀 t h e g r i n n i n g e m o j i
如果我们尝试使用 charAt()
方法打印相同的字符串,我们会得到以下输出。
public class Demo
{
public static void main(String args[])
{
String s = "😀 the grinning emoji";
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
System.out.print(c + " ");
}
}
}
输出:
? ? t h e g r i n n i n g e m o j i
总结
字符串,就像数组一样,遵循从零开始的索引。我们可以使用 charAt()
方法从字符串中获取单个字符。我们还可以将字符串转换为字符数组,然后使用它们的索引获取单个字符。如果你希望单个字符作为字符串而不是字符,则使用 Character.toString()
或 String.valueOf()
方法来转换它。有时我们的字符串可能包含基本多语言平面之外的字符。在这种情况下,我们可以使用 codePointAt()
方法而不是 charAt()
方法。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。