Java: How to split a string by a number of characters?(Java:如何将字符串拆分为多个字符?)
问题描述
我试图在线搜索以解决这个问题,但我没有找到任何东西.
I tried to search online to solve this question but I didn't found anything.
我写了以下抽象代码来解释我在问什么:
I wrote the following abstract code to explain what I'm asking:
String text = "how are you?";
String[] textArray= text.splitByNumber(4); //this method is what I'm asking
textArray[0]; //it contains "how "
textArray[1]; //it contains "are "
textArray[2]; //it contains "you?"
splitByNumber 方法将字符串text"每 4 个字符拆分一次.我怎样才能创建这个方法??
The method splitByNumber splits the string "text" every 4 characters. How I can create this method??
非常感谢
推荐答案
我认为他想要的是将一个字符串拆分为大小为 4 的子字符串.然后我会在循环中这样做:
I think that what he wants is to have a string split into substrings of size 4. Then I would do this in a loop:
List<String> strings = new ArrayList<String>();
int index = 0;
while (index < text.length()) {
strings.add(text.substring(index, Math.min(index + 4,text.length())));
index += 4;
}
这篇关于Java:如何将字符串拆分为多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:如何将字符串拆分为多个字符?


- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01