zoukankan      html  css  js  c++  java
  • 转载:Java中split函数自己重写

    转载网址:http://grewaller.blog.163.com/blog/static/654105520078610913562/

     
     

    public class Jian{
    public static void main(String[] args) {
          String strInfo      = "22,22,33";      //测试字符串,可随意更换
          String strSplit = ",";             //测试分隔串,可随意更换
      
          //调用函数,返回打印数值
          String[] a = yang_split(strInfo, strSplit);
          for(int k = 0; k< a.length; k++) System.out.println("a["+k+"]: "+ a[k]);
            }

    //★★★★★★★★★★★★★★★★★★★      函数      ★★★★★★★★★★★★★★★★★★★
            public static String[] yang_split(String strInfo, String strSplit) {
    //第1步计算数组大小(6行程序)
          int size = 1;
          for(int k = 0; k < strInfo.length(); k++) {
           if (strInfo.substring(k,k+1) != null &&      (strInfo.substring(k,k+1)).equals(",")) {
            size++;
           }
          }

          String[] arrRtn      = new String[size]; //返回数组
          String       strTemp = "";                   //临时变量
    //-------------------------------------------------------
    //第2步给数组赋值(25行程序)
          int len       = strInfo.length();
          int index =0;    
          int i         = 0;
          while (len > 1){
           index = strInfo.indexOf(strSplit);
           if (index>0){
            strTemp = strInfo.substring(0, index);
            strInfo = strInfo.substring(index+1);
            arrRtn[i++] = strTemp;  
           } else if (index==0){
            strInfo = strInfo.substring(index+1);
            arrRtn[i++] = "";
           } else{
            break;           //极端情况,如"4556658"  
           }
           len = strInfo.length();
          }

            index = strInfo.indexOf(strSplit);   
          if(index == 0){
           arrRtn[i++] = "";
           arrRtn[i] = "";    
          }else{
           arrRtn[i] = strInfo;
          }

          return arrRtn;
           }
    }

    =====================================================================

    程序全部代码如上,效果及帖图如下图.

    1.分隔函数

    2.main函数和调用效果图1
     
     
    3.main函数和调用效果图2
     
     
     
    李建方法非常不错,简洁大方,如下:
     
     
    //李建方法:
    //★★★★★★★★★★★★★★★★★★★    函数    ★★★★★★★★★★★★★★★★★★★
          public static String[] lj_split(String strInfo, String strSplit) {
           //第1步计算数组大小
        int size    = 0;
        int index = 0;
        do{
         size++;
         index++;
         index = strInfo.indexOf(strSplit ,index);
        }while(index!=-1);
        String[] arrRtn    = new String[size]; //返回数组
        //-------------------------------------------------------
        //第2步给数组赋值(25行程序)
        int startIndex = 0;
        int endIndex     = 0;     
        for(int i = 0; i < size; i++){
          endIndex = strInfo.indexOf(strSplit, startIndex);
           if(endIndex == -1) {
              arrRtn[i] = strInfo.substring(startIndex);
            } else {
             arrRtn[i] = strInfo.substring(startIndex, endIndex);
            }
         startIndex = endIndex+1;
        }
        return arrRtn;
         }
     
    截图:
     
  • 相关阅读:
    ESB数据发布思路
    Mongo客户端MongoVUE的基本使用
    ESB数据采集思路
    css3学习笔记之按钮
    发布Qt程序时别忘了带上plugins(codecs等)
    [Qt]Cannot retrieve debugging output
    Linux下编译opencv库[转]
    OpenCV库Windows下QT编译及使用
    ipad4自动下载了ios8的安装包,好几个G啊,不想更新,怎么删了呢?
    VC: error LNK2019:unresolved external symbol *** referenced in function ***的解决方案
  • 原文地址:https://www.cnblogs.com/lraa/p/2841761.html
Copyright © 2011-2022 走看看