Java查找指定字符串第一次或最后一次出现的位置
2016-04-02 18:28
447 查看
思路
1. 将输入的字符串转换为char 数组
2. 从第一个字符位置开始比较,并记录匹配的次数,如果匹配的次数等于查找字符串的长度就停止检索
3. 如果找不到匹配字符串的位置返回-1
/**
* Returns the index within this string of the rightmost occurrence of the
* specified substring.
*
* @param input
* @param query
*/
public static void getLastMatchingIndex(String input, String query) {
char[] inputChars = input.toCharArray();
char[] queryChars = query.toCharArray();
int inputLength = input.length();
int queryLength = query.length();
int inputIndex = inputLength - 1;
int queryIndex = queryLength - 1;
int matchingLenght = 0;
while (inputIndex >= 0 && queryIndex >= 0) {
if (inputChars[inputIndex] == queryChars[queryIndex]) {
inputIndex--;
queryIndex--;
matchingLenght++;
} else {
inputIndex = matchingLenght <= 0 ? (inputIndex - 1) : inputIndex;
queryIndex = queryLength - 1;
matchingLenght = 0;
}
System.out.println("i:" + inputIndex + ",q:" + queryIndex);
}
System.out.println("last matching index:" + (matchingLenght == queryLength ? (inputIndex + 1) : -1));
}
测试
first matching index:8
--->
1. 将输入的字符串转换为char 数组
2. 从第一个字符位置开始比较,并记录匹配的次数,如果匹配的次数等于查找字符串的长度就停止检索
3. 如果找不到匹配字符串的位置返回-1
/** * Returns the index within the given input string of the first occurrence * of the specified substring. * * @param input * @param query */ public static void getFirstMatchingIndex(String input, String query) { char[] inputChars = input.toCharArray(); char[] queryChars = query.toCharArray(); int inputLength = input.length(); int queryLength = query.length(); int inputIndex = 0; int queryIndex = 0; while (inputIndex < inputLength && queryIndex < queryLength) { if (inputChars[inputIndex] == queryChars[queryIndex]) { queryIndex++; inputIndex++; } else { inputIndex = inputIndex - queryIndex + 1; queryIndex = 0; } } int index = queryIndex == queryLength ? (queryLength > 1 ? inputIndex - queryLength : inputIndex - 1) : -1; System.out.println("first matching index:" + index); }
/**
* Returns the index within this string of the rightmost occurrence of the
* specified substring.
*
* @param input
* @param query
*/
public static void getLastMatchingIndex(String input, String query) {
char[] inputChars = input.toCharArray();
char[] queryChars = query.toCharArray();
int inputLength = input.length();
int queryLength = query.length();
int inputIndex = inputLength - 1;
int queryIndex = queryLength - 1;
int matchingLenght = 0;
while (inputIndex >= 0 && queryIndex >= 0) {
if (inputChars[inputIndex] == queryChars[queryIndex]) {
inputIndex--;
queryIndex--;
matchingLenght++;
} else {
inputIndex = matchingLenght <= 0 ? (inputIndex - 1) : inputIndex;
queryIndex = queryLength - 1;
matchingLenght = 0;
}
System.out.println("i:" + inputIndex + ",q:" + queryIndex);
}
System.out.println("last matching index:" + (matchingLenght == queryLength ? (inputIndex + 1) : -1));
}
测试
getFirstMatchingIndex("ssssdeffsdfsdf", "sdf");----->
first matching index:8
getFirstMatchingIndex("ssssdeffsdfsdf", "sdf");
--->
相关文章推荐
- JAVA printWriter中write()和println()区别
- 职责链模式(Chain of Responsibility)的Java实现
- java中Date与String的相互转化
- google更新5.0以后,用eclipse创建工程时出现的找不到styles.xml中的资源的问题
- 平时代码中收集整理的Eclipse快捷键
- JDK中的命令行工具
- 5 Java基础语法(方法,数组(初级))
- Java ClassLoader加载机制
- 给大家讲一个关于map和bean的故事(在SpringJdbc玩map被玩死)
- JAVA如何解析多层json数据
- java的内部类和匿名类
- SpringMVC实现Restful风格的WebService
- ZOJ 3714 Java Beans (华男神版)
- ZOJ 3714 Java Beans
- 解决eclipse开发Android时无法真机调试(使用魅族魅蓝note2实验)
- springMVC如何访问静态文件
- java面向对象中的方法重载与方法重写的区别
- Java序列化与反序列化
- 20145311 《Java程序设计》第5周学习总结
- Java 实现从数组删除指定位置元素