java - 如何用Java中的特定规则对给定字符串进行子字符串化

原文 标签 java string indexoutofboundsexception substring

这是我的代码,它在while上显示错误。索引超出范围的异常即将到来。子字符串应该是这样的


  • 每个字符串最多50个字符
  • 不得破坏数字-例如“4318、4466、486”是可以接受的,但“4318、4466、48”则不是
  • 子字符串应始终以一个空格开头,并以逗号“,”
  • 结尾


    有人可以帮我吗
     public class MyString {
    
    /**
     * @param args
    
     */
    static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486, 1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000";
    char space=' ';
     static char comma=',';
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int start=0, end=0;
        while (end-1 < input.length()){
    
        start=end;
        end= start+ 50;
    
        String output= input.substring(start,end);
        if(output.charAt(start)==' ' && output.charAt(end-1)==',')
        {
        System.out.println(output);
        } 
        else 
        {
    
        while(!(output.charAt(end-1)==','))
        {
    
        end--;
    
        }
        output= input.substring(start,end);
        System.out.println(output);
    
        }
        }
        }
    
    
    
    }
    

    最佳答案

    我对您的代码做了一些修改。

    class Ideone {
    
    static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486, 1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000";
    char space = ' ';
    static char comma = ',';
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int start = 0, end = 0;
        int temp = 0;
        while (end < input.length()) {
    
            start = end;
            end = Math.min(start + 50, input.length());
    
            String output = input.substring(start, end - 1);
            if (output.charAt(0) == ' ' && output.charAt(output.length() - 1) == ',') {
                System.out.println(output);
                end--;
            } else {
                temp = output.length() - 1;
                while (temp!=-1 && output.charAt(temp) != ',') {
                    temp--;
                }
                if(temp==-1)break;
                output = input.substring(start, start + temp + 1);
                System.out.println(output);
                end = start + temp + 1;
            }
    
        }
    }
    }
    

    Demo

    相关文章:

    java - meta-inf内部生成的 list 文件缺少类路径引用

    java - While循环检查重复字符

    c++ - 尝试将字符串的元素与循环和索引进行比较,但索引不会递增

    ruby - 在Ruby中,尝试将那些奇怪的引号转换为“常规”引号

    java - 不确定为什么数组超出限制JAVA

    java - 为什么ArrayIndexOutOfBoundsException不是编译时错误?

    java - 将节点列表中的元素扔到堆栈上

    javascript - Spring + JSP + Selectivity.js-如何从Java网站上构建的文件中读取选择性配置

    java - android使用class.forName的意图

    python - 在两个列表中查找匹配的子字符串