//字符串查找問題匯總
public class StringSearch {
/*
* search from left to right,return the position of first appear
* using indexOf()
* */
public static void searchByFirstPos(String s, String subStr){
if(s.isEmpty() || subStr.isEmpty())
return;
else{
int pos = 0;
pos = s.indexOf(subStr);
System.out.print(pos);
}
}
/*
* search from pos to right,return the position
* using indexOf(string, pos)
*/
public static void searchByAfterPos(String s, String subStr, int pos){
if(s.isEmpty() || subStr.isEmpty()|| pos < 0 || pos > s.length() - subStr.length())
return;
else{
System.out.print(s.indexOf(subStr, pos));
}
}
/*
* search from right to left, return the last appearance position
* using lastIndexOf(string)
*/
public static void searchByLastPos(String s, String subStr){
if(s.isEmpty() || subStr.isEmpty())
return;
else{
System.out.print(s.lastIndexOf(subStr));
}
}
/*
* search from right to left, return the last appearance position beforeee pos
* using lastIndexOf(string, pos)
*/
public static void searchByBeforeLastPos(String s, String subStr, int pos){
if(s.isEmpty() || subStr.isEmpty())
return;
else{
System.out.print(s.lastIndexOf(subStr, pos));
}
}
/*
* search from right to left, return the EVERY appearance position
* using indexOf(substr, pos),if find substr,changing pos, until the last of string
*/
public static void searchByAllPos(String s, String subStr){
if(s.isEmpty() || subStr.isEmpty())
return;
else{
int len = s.length();
int slen = subStr.length();
for(int i = 0 ; i < len;){
int temp = s.indexOf(subStr, i);
if(temp != -1){
System.out.print(temp);
i = temp + slen;
}
else
return;
}
}
}
/*
* search from right to left, return the EVERY appearance position by reverse order
* using lastIndexOf(substr, pos),if find substr,changing pos, until the length of string
*/
public static void searchByALLPosReverse(String s, String subStr){
if(s.isEmpty() || subStr.isEmpty())
return;
else{
int len = s.length();
int slen = subStr.length();
for(int i = len - 1 ; i >= 0;){
int temp = s.lastIndexOf(subStr, i);
if(temp != -1){
System.out.print(temp);
i = temp - 1;
}
else
return;
}
}
}
/*
* symbol 標(biāo)點(diǎn)符號
* 使用正則表達(dá)式 及替換標(biāo)點(diǎn)符號replaceAll(oldstring, newstring) 將每段文字分割split()
* Unicode 字符集七個字符屬性
* P:標(biāo)點(diǎn)屡限;
* L:字母巍耗;
* M:標(biāo)記符號(一般不會單獨(dú)出現(xiàn))晃择;
* Z:分隔符(比如空格镊靴、換行等)澡罚;
* S:符號(比如數(shù)學(xué)符號、貨幣符號等)恋谭;
* N:數(shù)字(比如阿拉伯?dāng)?shù)字话浇、羅馬數(shù)字等);
* C:其他字符
*/
public static void searchByPunctuation(String s){
if(s.isEmpty())
return;
else{
s = s.replaceAll("[\\pP]", "");//p:Unicode屬性媚赖, P:Unicode 字符集七個字符屬性之一:標(biāo)點(diǎn)字符
System.out.print(s);
}
}
public static void searchByLetter(String s){
if(s.isEmpty())
return;
else{
s = s.replaceAll("[\\pL]", "");//p:Unicode屬性
System.out.print(s);
}
}
public static void searchByNumber(String s){
if(s.isEmpty())
return;
else{
s = s.replaceAll("[\\pN]", "");//p:Unicode屬性
System.out.print(s);
}
}
public static void main(String[] args) throws Exception{
String test = "this is a string of testing 666,just a test 6,ok?";
String subtest = "test";
int position = 25;
StringSearch.searchByFirstPos(test, subtest);
StringSearch.searchByAfterPos(test, subtest, position);
StringSearch.searchByLastPos(test, subtest);
StringSearch.searchByBeforeLastPos(test, subtest, position);
StringSearch.searchByAllPos(test, subtest);
StringSearch.searchByALLPosReverse(test, subtest);
StringSearch.searchByPunctuation(test);
StringSearch.searchByLetter(test);
StringSearch.searchByNumber(test);
System.out.println("end!");
}
}