简体   繁体   English

在一行中仅打印字符串的前5个字符

[英]Printing only the first 5 characters of a string in one line

Im trying to print just the first 5 characters of a given string in one line. 我试图在一行中仅打印给定字符串的前5个字符。 My problem is the whole string is not being printed. 我的问题是整个字符串没有被打印。 Part of the end is being cut off. 结束部分被切断。

  int l = 0;
      int m = 5;
      for(int i = 0; i < (string.length()/5) ; i++)
      {
            System.out.println(string.substring(j,k));
            l = m;
            m = m + 5;

      }

Given string = "Hello world, my name is something which you want to" The result would be something like: 给定string = "Hello world, my name is something which you want to" ,结果将是这样的:

Hello
 worl
d, my

However, the last parts of the string is not being printed. 但是,字符串的最后部分没有被打印。

However, the last parts of the string is not being printed. 但是,字符串的最后部分没有被打印。

Yes, that's right - because of your loop condition. 是的,没错-由于您的循环条件。 You're iterating (string.length()/5) times - which rounds down. 您正在迭代(string.length()/5)次-四舍五入。 So if the string has 12 characters, you'll only iterate twice... leaving out the last two letters. 因此,如果字符串包含12个字符,则只需迭代两次,就省去了最后两个字母。

I would suggest solving this slightly differently - get rid of the l and m variables (which I assume you meant to use in your substring call - you never declare j or k ) and use the variable in the for loop instead. 我建议解决这个问题略有不同-摆脱lm变量(我假设您打算在substring调用中使用-永远不要声明jk ),而应在for循环中使用该变量。 You need to make sure that you don't try to use substring past the end of the string though - Math.min is handy for that: 但是,您需要确保不要尝试在substring末尾使用substring字符串Math.min对此非常方便:

for (int sectionStart = 0; sectionStart < string.length(); sectionStart += 5) {
    int sectionEnd = Math.min(string.length(), sectionStart + 5);
    System.out.println(string.substring(sectionStart, sectionEnd);
}

From your question what I understood is, you need output like 根据您的问题,我了解的是,您需要输出

Hello

 worl

d, my 

name 

is so

String str="Hello world, my name is something which you want to";
    for(int i=0;i<str.length();i++)
    {
        if(i%5==0 && i!=0)
        {
            System.out.println("");
        }
        System.out.print(str.charAt(i));


    }

Adding one more Approach 再增加一种方法

String str="Hello world, my name is something which you want to";
    for(int i=0,j=0;i<str.length();)
    {
        if(j<str.length() && (str.length()-j)>5)
        {j=i+5;}
        else
        {j=str.length();}
        System.out.println(str.substring(i,j));
        i+=5;
    }

Output: 输出:

Hello

 worl

d, my

 name

 is s

ometh

ing w

hich 

you w

ant t

o

Your variables are all over the place. 您的变量无处不在。 You have i , j , k , l , m ; 你有ijklm ; some of which are not defined in the code you present. 其中一些未在您提供的代码中定义。

But you should only have one: beginning of substring, say i . 但是您应该只有一个:子串的开头,说i End of substring is always 5 more: (i + 5) . 子字符串的结尾总是多5个: (i + 5) Then increment it by 5 each loopthrough. 然后每个循环将其增加5。

Simple Recursive method 简单的递归方法

void test(String t){

        if(t.length() > 4){
        String o = t.substring(0,5);
        System.out.println(o);
        String x = t.substring(5,t.length());
        test(x);
        }
        else{
            System.out.println(t);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM