My split function in Java
来源:互联网 发布:python八数码算法 编辑:程序博客网 时间:2024/06/05 04:26
This program aims for split an input string by specified characters, like ',' ' ' and so on.
import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class MyTokenClass {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubMyTokenClass mytoken = new MyTokenClass();String str = "a, b, c, da, dc, m#";str = mytoken.getInput();List<String> strList = mytoken.myTokenMethod(str, ',');for(String strObj:strList){System.out.print(strObj+" ");}mytoken.sortList(strList);}public String getInput() throws IOException{InputStreamReader r = new InputStreamReader(System.in);int c;c= r.read();StringBuffer inputStr = new StringBuffer("");while(c != '#' && c != -1){inputStr.append((char)c);c= r.read();}return inputStr.toString();}public List<String> myTokenMethod(String str, char splitChar){List<String> strList = new ArrayList<String>();int length = str.length();int startIndex = 0;int endIndex = 0;for(int i = 0; i < length; ++i){if(str.charAt(i) == splitChar){if(i == 0){endIndex = startIndex = i+1;}else{endIndex = i-1;}if(str.substring(startIndex, endIndex+1).replaceAll(" ", "").equals("") == false)strList.add(str.substring(startIndex, endIndex+1).replaceAll(" ", ""));startIndex = i+1;}else{endIndex++;}}strList.add(str.substring(startIndex, endIndex+1).replaceAll(" ", ""));return strList;}public void sortList(List<String> strList){System.out.println("\nAfter sorting("+strList.size()+"):");Collections.sort(strList);for(String std:strList){System.out.println(std);}}}
代码运行结果:
0 0
- My split function in Java
- Deprecated: Function split() is deprecated in ... 解决办法
- split "|" notes in java
- PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法
- php中“Deprecated: Function split() is deprecated in”错误解决办法
- PHP问题:Deprecated: Function split() is deprecated in解决方法
- my own split
- My Java 8 in Action
- Greedy Snake In Java ----My First Project
- Oracle Str Split Function
- php split function
- MySQL Split String Function
- Linux 内核可用的字符串分割函数 strsep (String Split Function strsep in Linux Kernel)
- ORA-06575: Package or function SPLIT is in an invalid state
- How can a member function in my derived class call the same function from its base class?
- How Function Interfaces Work in Java 8?
- SQL_UDF:Split like VB function
- sql and csharp: Split Function
- hbase 实战case1
- (15.1.11.1)听力之长对话
- 黑客是怎样入侵你的网站的
- hdoj 2717 Catch That Cow(一维坐标广搜)
- MZL's simple problem(STL multiset的应用)
- My split function in Java
- 17、C语言和设计模式(中介者模式)
- 在醒着的时间里,追求你认为最有意义的
- 黑客步骤
- 【CF149D】【Coloring Brackets】
- C语言字符串,字符转数字,数字转字符(转)
- Linux磁盘管理
- POJ3740
- hdu 1061 Rightmost Digit(快速幂)