Java:如何用一些字符拆分字符串?

编程入门 行业动态 更新时间:2024-10-13 06:13:20
本文介绍了Java:如何用一些字符拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在线搜索来解决这个问题,但我没有找到任何东西。

I tried to search online to solve this question but I didn't found anything.

我写了以下抽象代码来解释我问: / p>

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每4个字符分割一个字符串text。如何创建此方法

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:如何用一些字符拆分字符串?

本文发布于:2023-11-30 07:08:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何用   字符   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!