java - Finding the original index after stripping a string -


i have function following:

int getindex(string noisystring) {     string quietstring = noisystring.replaceall("[^a-z]", "");      int quietstringindex = findindexinquietstring(quietstring);      return originalindexinnoisystring; // ??? } 

after stripping string of non alphabetical characters, find arbitrarily chosen index inside stripped string. how can convert index 1 can used unstripped string?

it sounds trying index in non-filtered string of same character @ chosen index in filtered string.

(ie. have string s1 = "abc123def" s1.replaceall() = "abcdef". want original index of character @ index 4 in filtered string. character @ index 4 in filtered string e. index value in unfiltered string 7.)

the simplest brute force way use counter go through string keeping track of index to, meanwhile having separate counter variable keep track of how many characters have been passed valid filtered string.

public static int getoriginalindex(string s, int index){      if (index > s.replaceall("[^a-z]", "").length()) {         throw new illegalargumentexception("index invalid");     }      int counter;     int validcharcounter = 0;      (counter = 0; counter < s.length() && validcharcounter < index; counter++) {          if (s.charat(counter) >= 'a' && s.charat(counter) <= 'z')             validcharcounter++;     }      return counter; } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -