String Matching: The Simple way to implement String Matching
2012-05-10 14:49
323 查看
public static int match(String source, String pattern) { char[] sourceArray = source.toCharArray() ; char[] patternArray = pattern.toCharArray() ; int source_length = sourceArray.length ; int pattern_length = patternArray.length ; int source_index = 0; int pattern_index = 0; int foundtimes = 0 ; while(source_index < source_length) { if(sourceArray[source_index]==patternArray[pattern_index]) //equals { if(pattern_index == pattern_length-1) { int start = source_index - pattern_index ; int end = source_index - pattern_index+pattern_length ; System.out.println(source.substring(start,end)); foundtimes++ ; pattern_index = 0 ; continue ; } source_index++; pattern_index++; } else //not equals { source_index = source_index - (pattern_index-1); pattern_index = 0; } }//end while return foundtimes ; }
Reference:http://saturnman.blog.163.com/blog/static/5576112010969957130/
相关文章推荐
- String Matching: The Simple way to implement String Matching
- What is the fastest, case insensitive, way to see if a string contains another string in C#?
- The most easily way to set up a HTTP for simple simulation
- Simple way to repeat a String in java
- socket interface to implement a simple string conversion service over TCP
- What is the best way to implement a heartbeat in C++ to check for socket connectivity?
- 笔试题:Suggest a simple and fast way to multiply the input integer digit by 7。
- Simple way to implement caching in ASP.NET Web API
- Very good way of formatting data. To implement the interface System.Windows.Data.IValueConverter
- How to check the first character in a string in unix
- The different way to start a service(Bind_auto_create will just create a service and the service will exit when unbind)
- Exercises5-12 511-Do You Know the Way to San Jose?
- Go语言学习之reflect包(The way to go)
- The best way to handle the LazyInitializationException
- (中文版)How To Ask Questions The Smart Way(转载)
- How To Compile A Kernel - The Ubuntu Way
- String cannot be resolved to a type - The method getContextPath() from the type HttpServletRequest r
- Implement atoi to convert a string to an integer.
- win8安装 Matlab,运行出现 this application has requested the runtime to terminate it in an unusual way
- The way to study F2E/前端工程师学习之路