java日常笔记-字符串查找

//字符串查找问题汇总

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!");

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,352评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,588评论 19 139
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,248评论 0 6
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 5,945评论 0 2
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,972评论 18 399