写一个函数,要求输入一个字符串和一个字符长度,对该字符串进行分隔小弟确实不知怎么实现,希望大家指教下,麻烦讲的详细点,谢谢了

解决方案 »

  1.   

    来个简单的,
    我觉得lz没有把题目说清楚,或者说全。public class SplitString {
    public static void main(String args[]){
    System.out.println(split("11111", 4));
    }

    public static String split(String str, int size){
    if(size < 0){
    System.out.println("size is illegal.");
    return null;
    }else if(size >= str.length()){
    return str;
    }else{
    return str.substring(0, size);
    }
    }
    }
      

  2.   

    上一个没考虑传入的字符串是为null的情况:
    /**
     * @author bzwm
     *
     */
    public class SplitString {
    public static void main(String args[]) {
    System.out.println(split("11111", 4));
    } public static String split(String str, int size) {
    if (size < 0) {
    System.out.println("size is illegal.");
    return null;
    } else if (str != null && size >= str.length()) {
    return str;
    } else if (str != null) {
    return str.substring(0, size);
    } else {
    return null;
    }
    }
    }
      

  3.   

    小弟来实现下5楼的意思,不知道是不是LZ想问的。小弟自己调试结果正确。如有那些需要改进请指出。
    import java.io.BufferedReader;
    import java.io.*;public class ToSubString { public static void main(String[] args) {
    System.out.print("请输入要分解的字符串: ");
    String putInString = putIn();
    System.out.print("请输入要分割的字符长度: ");
    int intI = Integer.parseInt(putIn());
    int i = 0;
    int j = intI;
    int m = putInString.length();
    if( intI < m ) {
    while( j <= m ){
    System.out.print( putInString.substring(i,j) + "   ");
    i = i + intI;
    j += intI;
    }
    if( i<m ) {
    System.out.println(putInString.subSequence(i, m));
    }
    }else {
    System.out.println(putInString);
    }
    }
    //键盘输入方法。
    private static String putIn() {
    String str = "";
    try{
    BufferedReader br = 
                                 new BufferedReader(new InputStreamReader(System.in));  
    str = br.readLine();
    }catch(IOException e){
    }
    return str;
    }
    }
      

  4.   

    我晕。直接贴过来竟然变成这个格式的。
    重新编辑下格式如下://两个包只用一个java.io.*就可以了。刚开始修错添重了。import java.io.*; public class ToSubString {   public static void main(String[] args) { 
          System.out.print("请输入要分解的字符串: "); 
          String putInString = putIn(); 
          System.out.print("请输入要分割的字符长度: "); 
          int intI = Integer.parseInt(putIn()); 
          int i = 0; 
          int j = intI; 
          int m = putInString.length(); 
          if( intI < m ) { 
             while( j <= m ){ 
                System.out.print( putInString.substring(i,j) + "  "); 
                i = i + intI; 
                j += intI; 
             } 
             if( i <m ) { 
             System.out.println(putInString.subSequence(i, m)); 
             } 
          }else { 
             System.out.println(putInString); 
          } 
       } 
       //键盘输入方法。 
       private static String putIn() { 
         String str = ""; 
         try{ 
             BufferedReader br = 
                new BufferedReader(new InputStreamReader(System.in));  
             str = br.readLine(); 
         }catch(IOException e){} 
         return str; 
       } 
    } //忽然发现也可以对中文分组。嘿嘿。第一次在CSDN发程序贴。请多指教。
      

  5.   


      StringSplit函数实现分组.返回值是一个ArrayList里面装有已分完组的字符串
    import java.util.ArrayList;
    public class temp {
    public static void main(String[] args) {
    for (String s : StringSplit("abcdefghijklmnopqrstuvwxyz", 4))
    System.out.println(s);
    } static ArrayList<String> StringSplit(String s, int n) {
    ArrayList<String> splits = new ArrayList<String>();
    assert s != null &&  n > 0; 
    int pos;
    for (pos = 0; pos + n < s.length(); pos += n) {
    splits.add(s.substring(pos, pos + n));
    }
    splits.add(s.substring(pos, s.length()));
    return splits;
    }
    }
      

  6.   

    /**
     * add char from right of string/keep char from site-0 of string
     * 
     * @param s
     * @param def
     * @param length
     * @return
     */
    public String add2StrLeft2Right(String s, String def, int length) {
    if (s == null) {
    s = "";
    }
    if (def == null || def.length() < 1) {
    def = " ";
    }
    if (s.length() >= length) {
    s = s.substring(0,length);
    } else {
    StringBuffer bf = new StringBuffer();
    bf.append(s);
    for (int i = 0; i < (length - s.length()); i++) {
    bf.append(def);
    }
    s = bf.toString();
    }
    return s;
    } /**
     * add char from left of string/keep char from last of string
     * 
     * @param s
     * @param def
     * @param length
     * @return
     */
    public String add2StrRight2Left(String s, String def, int length) {
    if (s == null) {
    s = "";
    }
    if (def == null || def.length() < 1) {
    def = " ";
    }
    if (s.length() >= length) {
    s = s.substring(s.length() - length);
    } else {
    StringBuffer bf = new StringBuffer();
    for (int i = 0; i < (length - s.length()); i++) {
    bf.append(def);
    }
    bf.append(s);
    s = bf.toString();
    }
    return s;
    }
      

  7.   

    这个方法将输入的字符串(包括占两个字节的汉字)按照输入的字节数分割。
    例如:str=“ABC我DE”,subBytes=4则应该输出ABC,半个字节的“我”不输出。
    public String subString(String str, int subBytes)
    {
    int bytes = 0; // 用来存字符串的字節数
    for(int i = 0; i < str.length(); i++)
    {
    if (bytes == subBytes)
    {
    return str.substring(0, i);
    }
    char c = str.charAt(i);
    if (c < 256)
    {
    bytes += 1; // 英文字符的字?数看作1
    }
    else
    {
    bytes += 2; // 中文字符的字?数看作2
    if (bytes - subBytes == 1)
    {
    return str.substring(0, i);
    }
    }
    }
    return str;
    }