What is the easiest/best/most correct way to iterate through the characters of a string in Java?(在Java中遍历字符串字符的最简单/最好/最正确的方法是什么?)
问题描述
在 Java 中遍历字符串字符的一些方法是:
Some ways to iterate through the characters of a string in Java are:
- 使用
StringTokenizer? - 将
String转换为char[]并对其进行迭代.
- Using
StringTokenizer? - Converting the
Stringto achar[]and iterating over that.
最简单/最好/最正确的迭代方式是什么?
What is the easiest/best/most correct way to iterate?
推荐答案
我使用 for 循环迭代字符串并使用 charAt() 获取每个字符来检查它.由于 String 是用数组实现的,所以 charAt() 方法是一个常数时间的操作.
I use a for loop to iterate the string and use charAt() to get each character to examine it. Since the String is implemented with an array, the charAt() method is a constant time operation.
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char
}
这就是我会做的.这对我来说似乎是最简单的.
That's what I would do. It seems the easiest to me.
就正确性而言,我认为这里不存在.这完全取决于您的个人风格.
As far as correctness goes, I don't believe that exists here. It is all based on your personal style.
这篇关于在Java中遍历字符串字符的最简单/最好/最正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Java中遍历字符串字符的最简单/最好/最正确的方法是什么?
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
