"This is my first sentence. thanks for share!"
把上面的句子按截出来,每次截10个字符,而且不可把单词分开截。 

解决方案 »

  1.   


    package newToday;public class splitString { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub String str="This is my first sentence. thanks for share!";
    spiltIt(str);
    }
    public static void spiltIt(String str)
    {
    int count=0,i;
    String reg="\\b";
    String[]strs=str.split(reg);
    for(i=0;i<strs.length;i++)
    {
    if(strs[i].equals(" "))
    {
    System.out.println("空格");
    continue;
    }
    count+=strs[i].length();

    if(count>=10)
    {
    break;
    }
    }
    System.out.println("--"+strs[2]+"--");
    StringBuffer sb=new StringBuffer();
    for(int j=0;j<=i;j++)
    {
    sb.append(strs[j]);
    //sb.append(" ");
    }
    System.out.println(sb);
    }}
    这个符合吗??
      

  2.   

    LZ的意思如果字符长度不能拆分单词,比如:The name very ...
    第一次取得的结果是 The name 
      

  3.   

    按LZ的说法,如果每次必需10个字符,而不是少于等于10个字符的话,
    那么再加上后面的条件
    ---->此题无解!
      

  4.   


    /**
     * @author Administrator
     *
     */
    public class SplitWord { /**
     * @param args
     */
    public static void main(String[] args) {
    String words  ="This is my first sentence. thanks for share! i'm successd please give me money 100 RMB!OK?";
    StringBuffer sb = new StringBuffer();
    int count = 0 ;
    for (int i = 0; i < words.length(); i++) {
    if(count < words.length()){
    char a = words.charAt(count);
    count++;
    sb.append(a);
    if((count % 10 == 0 && count != 0)||(count == words.length())){
    char preCh= words.charAt(count-1);
    char nextCh=((count+1 < words.length()))? words.charAt(count+1) : ' ';
    if(preCh == ' ' || nextCh == ' ' || a == ' '){
    System.out.println(sb);
    }else{
    for(int j = count;j<= words.length() ;j++){
    char currentCh_= words.charAt(j);
    char preCh_= words.charAt(j-1);
    char nextCh_=((j+1)<words.length())? words.charAt(j+1) :' ';
    sb.append(currentCh_);
    count++;
    if(currentCh_ == ' ' || preCh_ == ' ' || nextCh_ == ' '){
    String strsb = sb.toString();
    System.out.println(strsb);
    break;
    }
    }
    }
    sb.delete(0, words.length());
    }
    }else{
    break;
    }
    }
    }}
      

  5.   


    /*
    This is my 
    first sentence.
     thanks
     for share!
     i'm successd
     please
     give 
    me money 100
     RMB!OK?
    */楼上的,超过十个字符要回退,输出第二行...
      

  6.   


                    String text = "This is my first sentence. thanks for share!";
    int head = 0; //记录首位置
    int foot = 9;//记录尾
    while(foot <= text.length()) {
    //判断尾是否为空格,如果为空格就foot向后退一格
    if(text.charAt(foot+1) != ' ') {
    foot--;
    continue;
    }
    String str = text.substring(head,foot+1);
    System.out.println(str.trim());
    head = foot+1;
    foot = head+10;
    }

    //输出最后的一部分
    System.out.println(text.substring(head).trim());
      

  7.   

    String text = "This is my first sentence. thanks for share!";
    int head = 0; //记录首位置
    int foot = 9;//记录尾
    while(foot <= text.length()) {
    //判断尾是否为空格,如果为空格就foot向后退一格
    if(text.charAt(foot+1) != ' ') {
    foot--;
    continue;
    }
    String str = text.substring(head,foot+1);
    System.out.println(str.trim());
    head = foot+1;
    foot = head+10;
    }

    //输出最后的一部分
    System.out.println(text.substring(head).trim());