Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the principle of indexOf and what is the implementation of Java and javascript and python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to achieve the principle of indexOf and Java and javascript and python implementation is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Brief introduction

The meaning of indexOf: given a string to match the subscript of another string, if matching, return subscript, if not, return-1, in fact, the principle is relatively simple, if you need to achieve, then what should be done?

Principle

Now given the matching string A, the original string B, for example, the matching string An equals "called practice", and the original string B equals "call while practicing, I like to call practice". You may find at a glance that "call practice" is the last two characters. we use B as a loop, a word to match, first find "call", find the counter plus 1, and then continue to find "practice" and find that the next character is not "practice". Counter zero, re-start from the "called" search, now find the penultimate character of B, the next character is "practice" calculator plus 1, at this time the counter is equal to 2, which is exactly the length of the A string, indicating that it has been found. the search principle is such a process. Let's implement it in the way of Java,javascript,python.

Note: the following code is not checked, for reference only, python is index method, the implementation principle is the same, but can not be found will report an error!

Implement Java implement public static void main (String [] args) {String orgin = "call while practicing, I like to practice"; String serach = "call practice"; int index = indexOf (orgin,serach); System.out.println (index);} / * * indexOf algorithm principle * @ param orgin original string B = "call while practicing, I like to practice" * @ param serachString match string A = "call" * @ return int subscript * / public static int indexOf (String orgin,String serachString) {char [] chars = orgin.toCharArray (); char [] sChars = serachString.toCharArray (); / / return string subscript int index =-1; / / match string counter to query whether it matches the complete string int s_index = 0 / / global counter used to calculate subscript int move = 0; for (int item0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report