【问题标题】:Given a string, generate all 2-consecutive characters, 3-consecutive characters...and so on up to ( str.length()-1 )-consecutive characters给定一个字符串,生成所有 2 个连续的字符、3 个连续的字符……等等直到 ( str.length()-1 ) 个连续的字符
【发布时间】:2020-05-16 17:44:41
【问题描述】:

我需要在 Java 中完成以下工作:

1. 取一个字符串。

2. 生成字符串中所有2个连续的字符。

3. 生成字符串中所有 3 个连续的字符。

4. 以此类推,直到最后一代,将是 (str.length())-1-连续字符。

为了进一步澄清,请考虑字符串hello!。该字符串的长度为6。注意最后一代是 5 个连续的字符。输出应该是:

he // 2-consecutive
el // 2-consecutive
ll // 2-consecutive
lo // 2-consecutive
o! // 2-consecutive

hel // 3-consecutive
ell // 3-consecutive
llo // 3-consecutive
lo! // 3-consecutive

hell // 4-consecutive
ello // 4-consecutive
llo! // 4-consecutive

hello // 5-consecutive
ello! // 5-consecutive

这是我尝试过的:

String str = "hello!";
            int len = str.length();
            for (int set = 2; set <= (len-1); set++) {
            for (int n = 0; n <= (len-1); n++) {
                for (int k = 0; k <= n; k++) {
                    System.out.print(str.charAt(k));
                    }
                    System.out.println();
                }
}

代码给出了以下输出,这与我之前希望的完全不同:

h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!
h
he
hel
hell
hello
hello!

This question 会帮助我,但我完全不熟悉 ruby​​。

【问题讨论】:

    标签: java string combinations


    【解决方案1】:

    您不需要两个循环。您可以使用substring 属性。如果您真的想使用另一个循环,您当然可以用答案中的循环替换substring,以及一个简单的if 条件以确保我们不会超过输入的最后一个索引。

        String str = "hello!";
        int len = str.length();
        int gap = 2; // This determines the consecutive character size.
        for( int set = 0; set <= len; set++ )
        {
            if( ( set + gap ) <= len )
            {
                String subString = str.substring( set, set + gap );
                System.out.println( subString );
            }
        }
    

    这就是你可以使用第二个循环而不是substring的方法

        for( int set = 0; set <= len; set++ )
        {
            if( ( set + gap ) <= len )
            {
                String result = "";
                for( int i = set; i < set + gap; i++ )
                {
                    result += str.charAt( i );
                }
                System.out.println( result );
            }
        }
    

    如果您在循环中使用字符串连接。请记住,不建议这样做。请改用StringBuilder。对于您的方案,这两种方法都可以使用。

    【讨论】:

    • 谢谢。奇迹般有效。我正在通读它以获得全面的理解。太棒了!
    【解决方案2】:

    只是想指出您的原始代码非常接近正确。这个版本产生了预期的结果:

    String str = "hello!";
    int len = str.length();
    for (int set = 2; set <= (len - 1); set++) {
        for (int n = 0; n <= (len - set); n++) {     // changed (len - i) to (len-set)
            for (int k = 0; k < set; k++) {          // changed (k <= n)  to (k < set)
                System.out.print(str.charAt(n+k));   // changed (k)       to (n+k)
            }
            System.out.println();
        }
    }
    

    按照 Klaus 的建议,您也可以使用子字符串:

    for(int set = 2; set < len; set++) {
        for(int n=0; n <= len-set; n++) {
            System.out.println(str.substring(n, n+set));
        }
    }
    

    【讨论】:

    • 谢谢你。感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2018-10-28
    • 2015-04-01
    • 2022-11-14
    • 2012-02-11
    相关资源
    最近更新 更多