gpt4 book ai didi

java - 如何按用户的分割大小在 JOptionpane 中显示分割字符串

转载 作者:行者123 更新时间:2023-12-01 17:55:17 25 4
gpt4 key购买 nike

您好,如何在 JOptionPane 中显示我的拆分字符串?我的窗口不断打印/显示我的字符串 1 by 1 ,我希望它们根据用户的分割大小打印/显示

    String letters, splitSize;

letters = JOptionPane.showInputDialog("Enter String: ");
final int numInLetters = letters.length();

splitSize = JOptionPane.showInputDialog("Enter Split Size");
int sizeSplit = Integer.parseInt(splitSize);

if (numInLetters % sizeSplit == 0) {

JOptionPane.showMessageDialog(null, "The Given String is" + letters);
JOptionPane.showMessageDialog(null, "The Split String are: ");

String []in_array;

in_array = letters.split("");
for (int i = 1; i <= in_array.length; i++) {


//what alternative way to show my split string here given by user's split size
JOptionPane.showMessageDialog(null, in_array[i-1]);


if (i % sizeSplit == 0) {

JOptionPane.showMessageDialog(null, "");

最佳答案

我不太确定你想要完成什么,但我认为这可能就是你想要的:

String letters = JOptionPane.showInputDialog("Enter String: ");

String splitSize = JOptionPane.showInputDialog("Enter Split Size");
int sizeSplit = Integer.parseInt(splitSize);

List<String> list = new ArrayList<>();
int idx = 0;
while (idx < letters.length()) {
int toIdx = Math.min(idx + sizeSplit, letters.length());
list.add(letters.substring(idx, toIdx));
idx = toIdx;
}

JOptionPane.showMessageDialog(null, "The Split String are: " + System.lineSeparator() + String.join(System.lineSeparator(), list));

关于java - 如何按用户的分割大小在 JOptionpane 中显示分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com