[英]How do I check every value in a Java string?
If I had a string, say: 如果我有一个字符串,请说:
String name;
Is there a way I can check each letter with a loop? 有没有办法可以循环检查每个字母? So if they enter a name can I check what each letter is?
因此,如果他们输入名称,我可以检查每个字母是什么吗?
There are several ways to do it - you can do a for
loop with an index, or a foreach
loop on the char[]
array: 有几种方法可以执行-您可以
for
索引for
循环,也可以对char[]
数组进行foreach
循环:
for (int i = 0 ; i != s.length() ; i++) {
char c = s.charAt(i);
....
}
for (char c : s.toCharArray()) {
....
}
In addition to checking each letter separately, you can check all letters at once (or any subset that you'd like) with regular expressions . 除了分别检查每个字母之外,您还可以使用正则表达式一次检查所有字母(或所需的任何子集)。
使用String.toCharArray()
函数。
char[] chars = name.toCharArray();
for (int i =0; i < chars.length; i++)
{
System.out.println(chars[i]);
}
This should do 这应该做
for ( int i=0 ; i < name.length(); i++)
{
name.charAt(i);
}
Also read this http://docs.oracle.com/javase/tutorial/java/data/manipstrings.html 另请阅读此http://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.