//字符串查找问题汇总
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 标点符号
* 使用正则表达式 及替换标点符号replaceAll(oldstring, newstring) 将每段文字分割split()
* Unicode 字符集七个字符属性
* P:标点;
* L:字母;
* M:标记符号(一般不会单独出现);
* Z:分隔符(比如空格、换行等);
* S:符号(比如数学符号、货币符号等);
* N:数字(比如阿拉伯数字、罗马数字等);
* C:其他字符
*/
public static void searchByPunctuation(String s){
if(s.isEmpty())
return;
else{
s = s.replaceAll("[\\pP]", "");//p:Unicode属性, P:Unicode 字符集七个字符属性之一:标点字符
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!");
}
}