zoukankan      html  css  js  c++  java
  • j2me MIDP2.0 下实现split函数

       /**
       * 分割字符串,原理:检测字符串中的分割字符串,然后取子串
        * 
    @param original 需要分割的字符串
        * @paran regex 分割字符串
        * 
    @return 分割后生成的字符串数组
        
    */
        
    public static String[] split(String original,String regex)
        {
              
    //取子串的起始位置
              int startIndex = 0;
              
    //将结果数据先放入Vector中
              Vector v = new Vector();
              
    //返回的结果字符串数组
              String[] str = null;
              
    //存储取子串时起始位置
              int index = 0;

              
    //获得匹配子串的位置
              startIndex = original.indexOf(regex);
              
    //System.out.println("0" + startIndex);
              
    //如果起始字符串的位置小于字符串的长度,则证明没有取到字符串末尾。
              
    //-1代表取到了末尾
              while(startIndex < original.length() && startIndex != -1)
              {
                    String temp 
    = original.substring(index,startIndex);
                    System.out.println(
    " " + startIndex);
                    
    //取子串
                    v.addElement(temp);

                    
    //设置取子串的起始位置
                    index = startIndex + regex.length();

                    
    //获得匹配子串的位置
                    startIndex = original.indexOf(regex,startIndex + regex.length());
              }

              
    //取结束的子串
    //          v.addElement(original.substring(index + 1 - regex.length()));
              v.addElement(original.substring(index));
              
    //将Vector对象转换成数组
              str = new String[v.size()];
              
    for(int i=0; i<v.size(); i++)
              {
                    str[i] 
    = (String)v.elementAt(i);
              }

              
    //返回生成的数组
              return str;
        }
  • 相关阅读:
    eslint 验证vue文件 报错 unexpected token =解决方法
    启动3ds Max报 d3dx9_43.dll丢失 解决方法
    windows下webpack不是内部命令 解决方法
    修改node.js默认的npm安装目录
    tp5 重定向缺少index.php报错(No input file specified)
    PHP单表操作mysqli数据库类的封装
    php 常见图片处理函数封装
    php图像处理函数image_type_to_extension、image_type_to_mime_type 的区别
    kubernetes集群部署mysql 8.0
    Maven ResourceBundle.getBundle读取Properties异常MissingResourceException: Can't find bundlei解决方法
  • 原文地址:https://www.cnblogs.com/zhaoguo435/p/1804175.html
Copyright © 2011-2022 走看看