zoukankan      html  css  js  c++  java
  • 分享分割字符串

     public String[] strchange(String str, char ch)   {    

          int a = 0;   //存放ch的数量

          for (int i = 0; i < str.length(); ++i)      

          if (ch == str.charAt(i))       

          ++a;

           String[] arr = new String[a + 1];

          Integer[] arrint = new Integer[a];

          int b = 0;    //增量,实现同步

          for (int i = 0; i < str.length(); ++i)     

           if (ch == str.charAt(i)) {      

               arrint[b] = Integer.valueOf(i + 1);         ++b;    

           }

        int begin = 0;     int ab = 0;    //增量

        for (int i = 0; i < arrint.length; ++i) {    

           arr[ab] = str.substring(begin, arrint[i].intValue() - 1); 

           begin = arrint[i].intValue();       ++ab;  

           }

           arr[(arr.length - 1)] = str.substring(arrint[(arrint.length - 1)].intValue());

            return arr;

      }

    大致思想就是,先计算出字符串中,需要分割字符串的位置,然后依据存放位置的数组来遍历分割字符串

  • 相关阅读:
    Markdown基本语法
    面向对象
    LeetCode739 每日温度
    LeetCode155 最小栈
    LeetCode279 完全平方数
    LeetCode752 打开转盘锁
    LeetCode622 设计循环队列
    LeetCode200 岛屿的个数
    LeetCode61 旋转链表
    LeetCode138 复制带随机指针的链表
  • 原文地址:https://www.cnblogs.com/SATinnovation/p/3113297.html
Copyright © 2011-2022 走看看