寫在前面:
用split函數處理正則表達式時用到了Pattern類和Matcher類,Pattern類和Matcher類用法詳解可參考大神的博客:https://blog.csdn.net/zengxiantao1994/article/details/77803960.
spilt函數:
主要有三種情況:
1.只有一位普通字符;
2.兩位轉義字符;
3.正則表達式。
SplitUtil類
package num;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SplitUtil {
private static final String contant = ".$|()[{^?*+\\";
public static String[] splitMethod(String string, String regex){
char ch = 0;
int off = 0;
int next = 0;
ArrayList<String> list = new ArrayList();
//一位字符或是轉義字符
if(regex.length() == 1 && contant.indexOf(ch = regex.charAt(0)) == -1 ||
regex.length() ==2 && regex.charAt(0)=='\\' && ((ch = regex.charAt(1))-'0'|'9'-ch)<0 && (ch-'a' | 'z'-ch)<0 && (ch-'A'|'Z' -ch)<0)
{
while ((next = string.indexOf(ch,off)) > 0){
list.add(string.substring(off, next));
off = next + 1;
}
if(off == 0){
return new String[]{string};
}
list.add(string.substring(off, string.length()));
return interceptEmpty(list);
}
//正則表達式
return regexSplit(string, regex);
}
//正則表達式
private static String[] regexSplit(String string, String regex) {
int off = 0;
//將給定的正則表達式編譯到模式中
Pattern pattern = Pattern.compile(regex);
//創建給定輸入與此模式匹配的匹配器
Matcher m = pattern.matcher(string);
List<String> list = new ArrayList();
while (m.find()){
//m.start(): 返回第一個匹配字符的索引
list.add(string.substring(off, m.start()));
//System.out.println(off+" "+m.start()+" "+m.end());
list.add(string.substring(m.start(), m.end()));
//m.end(): 返回最後匹配字符之後的偏移量
off = m.end();
}
if(off == 0){
return new String[]{string};
}
list.add(string.substring(off, string.length()));
return interceptEmpty(list);
}
//截取空字符串
private static String[] interceptEmpty(List<String> list){
int resultSize = list.size();
while (resultSize>0 && list.get(resultSize-1).length() == 0){
resultSize--;
}
String[] strings = new String[resultSize];
return list.subList(0, resultSize).toArray(strings);
}
//測試方法
/*public void test() {
//測試regex只有一位,且不爲列出的特殊字符
String s1 = "gg,tge,hbfs,ijkd,,,";
String[] strings1 = splitMethod(s1, ",");
for (int i = 0; i < strings1.length; i++) {
System.out.println(strings1[i]);
}
//測試regex有兩位,第一位位轉義字符且第二位不是數字和字母
String s2 = "bb\'dn\'ags\'kl\'\'";
String[] strings2 = splitMethod(s2,"\\'");
for (int i = 0; i < strings2.length; i++) {
System.out.println(strings2[i]);
}
//測試正則表達式
String ss = "ac32dge533grhr139ljs343";
String[] strings = splitMethod(ss,"[\\d]+");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}*/
}
害,需要注意處理正則表達式的這裏:
//正則表達式
private static String[] regexSplit(String string, String regex) {
int off = 0;
//將給定的正則表達式編譯到模式中
Pattern pattern = Pattern.compile(regex);
//創建給定輸入與此模式匹配的匹配器
Matcher m = pattern.matcher(string);
List<String> list = new ArrayList();
while (m.find()){
//m.start(): 返回第一個匹配字符的索引
list.add(string.substring(off, m.start()));
//System.out.println(off+" "+m.start()+" "+m.end());
list.add(string.substring(m.start(), m.end()));
//m.end(): 返回最後匹配字符之後的偏移量
off = m.end();
}
if(off == 0){
return new String[]{string};
}
list.add(string.substring(off, string.length()));
return interceptEmpty(list);
}
list.add(string.substring(m.start(), m.end()));
這一句是截取了分隔符部分,spilt方法不會存儲分隔符。但是老師要求輸出分隔符 只處理了正則表達式這裏的輸出,前兩種情況不輸出分隔符。
splitmain類:
package num;
public class splitmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
SplitUtil xx = new SplitUtil();
String[] strings = xx.splitMethod("asb#?Gfj%123*hj", "[#?%*]");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
}
預覽輸出如下:
中間爲什麼會多一個空格呢?因爲“asb#?Gfj%123*hj”中,分隔符“#”與“?”之間爲null,自然沒有輸出了。要去掉的話,在輸出的時候判斷一下是否爲空。
寫在後面:
意大利作家卡爾維諾說:
我對任何唾手而得,快速,出自本能,即興,含混的事物沒有信心。
我相信緩慢,平和,細說長流的力量,踏實,冷靜。
我不相信缺乏自律精神和不自我建設,不努力,可以得到個人或集體的解放。
而那些真正讓人變好的選擇,過程都不會很舒服,甚至痛苦。
比如鍛鍊身體,比如學習一門技能。唯有你內心足夠渴望,你纔有頑強的意志力去支撐、鼓舞着自己去堅持。並且把這種堅持變成生命中和喝水喫飯睡覺一樣重要的事情。
靜水深流,源源不斷,終能匯成大江大河。而這,就是自律的頂級力量。