Can a Java String indexOf () function look for multiple characters?

I am trying to reduce the execution time of the program I'm working on, and I want to find an easier way to find the index of a character with an integer value.

At the moment, my function keeps track of every character in the string and returns the index index of the first integer it finds. As an example, if I had a string JohnCena1237728394712(yes, this first line I was thinking about), the function should return 8, because the first integer that appears on line 1 is at index 8. It should be the first loop however, through each character before this find this index, and it is sometimes expensive.

If this helps in trying to find an easier way to do this, I can be sure that the function will always be in the format of [letters] "+" [numbers] ", so I just have to find the end of the (supposedly random) segment of letters so get what i want.

What I would like to do is use indexOf (), so I don’t have to use a loop, but I don’t know how to use a function without ten if statements (which, of course, will defeat the goal).

Is there a way to check multiple integers at once when using the indexOf () function or some other function?

+4
source share
5 answers

You can do this with regex.

Sort of:

 Pattern pattern = Pattern.compile("\\d");
 Matcher matcher = pattern.matcher(inputString);
 if(matcher.find()) {
     return matcher.first();
 }
 return -1;

. for . , , , .

 for(int i=0; i<str.length; i++) {
     char c = str.charAt(i);
     if(c >='0' && c <= '9') {
         return i;
     }
 }
 return -1;

, - , - , .

, , String - map-reduce-ish. , .

+2

- .

String str = "Hello world";
str.matches("\\w+ \\w+");                   // returns true

Pattern Matcher.

String line = "Hello amazing world";
String pattern = "(\\w+) \\w+ (\\w+)";

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);

if (m.find( )) {
     System.out.println(m.group(1));        // Prints "Hello"
     System.out.println(m.group(2));        // Prints "world"
}
+1

ascii , ,

. , 1

String str = "hello123";
for(int i=0; i < str.length(); i++) {
    char c = str.charAt(i);
    if(c >= '0' && c <= '9') {
         // Code goes here
    }
}

:

  • indexOf() , O (n ^ 2)
  • , , ( , )
0

.charAt(i), ... char , FOR, :

char[] chars = string.toCharArray();
for (int i=0; i<chars.lenght; i++) {
    if(chars[i] >= '0' && chars[i] <= '9')
        return i; //Or whatever code you need.
}
0

If I understand your question correctly, you have a bunch of characters followed by a whole chain.

You can use the modified binary search to find the point in the line where the transition occurs. This should be useful if you need to parse very large strings. The worst performance is O (log n).

I encoded an example below:

public class binSearch {

    static int indexFinder(String s) {

        // this doesn't check for invalid inputs btw
        int beg = 0;
        int end = s.length() - 1;
        int mid;
        int iter = 0;
        while (beg < end) {
            System.out.println("Iteration: " + iter + "   String: " + s.substring(beg, end+1));
            mid = (end + beg) / 2;
            System.out.println("Mid: " + mid + "   char: " + s.charAt(mid));
            if (Character.isDigit(s.charAt(mid))) {
                if (Character.isDigit(s.charAt(beg))) {
                    return beg;
                }
                end = mid;
            } else {
                beg = mid + 1;
            }
            iter++;
        }
        return beg;
    }

    public static void main(String[] args) {
        System.out.println("Running");
        String[] strings = {"joohhhnnnnnCEEENNAAAAAA123829898", "efi1029082198"};

        for (String s : strings) {
            StringBuilder check = new StringBuilder();
            for(int i = 0; i < s.length(); i++) {
                check.append(i % 10);
            }
            System.out.println(s);
            System.out.println(check.toString() + "\n");
            System.out.println("First int index: " + indexFinder(s) + "\n\n");
        }
    }
}

Conclusion:

Running
joohhhnnnnnCEEENNAAAAAA123829898
01234567890123456789012345678901

Iteration: 0   String: joohhhnnnnnCEEENNAAAAAA123829898
Mid: 15   char: N
Iteration: 1   String: NAAAAAA123829898
Mid: 23   char: 1
Iteration: 2   String: NAAAAAA1
Mid: 19   char: A
Iteration: 3   String: AAA1
Mid: 21   char: A
Iteration: 4   String: A1
Mid: 22   char: A
First int index: 23


efi1029082198
0123456789012

Iteration: 0   String: efi1029082198
Mid: 6   char: 9
Iteration: 1   String: efi1029
Mid: 3   char: 1
Iteration: 2   String: efi1
Mid: 1   char: f
Iteration: 3   String: i1
Mid: 2   char: i
First int index: 3
0
source

Source: https://habr.com/ru/post/1655364/


All Articles