0

I'm looking for terminator characters in Strings using indexOf.

// character codes to look for
int[] defseps = new int[] {10, 13, 34, 36, 38, 39,41, 44, 59, 63, 93, 125};
int t = 999999999;  // big number,  We want first terminator 
int tempt = 0;

// loop through possible terminators
for (int x = 0; x < 12; x++) {
    tempt=str.indexOf(defseps[x]); // Get index of terminator
    if (defsepactivated[x] && tempt!=-1) {  // If active terminator found
        System.out.println("defsep used=" + defseps[x]);
        if (tempt < t) t = tempt; // Use this terminator if before previous found  
    }
}

This code finds terminators like & (38) and ] (93) but not double quote (34).

For example if str is : =THINGTHUNG";copyright &copy it finds the semi colon and & but not the double quote.

Much appreciate why that would be the case before I try other codings.

Nigele2
  • 33
  • 4
  • You can also loop through the String usign `toCharArray()` – Rob Audenaerde Nov 11 '14 at 12:43
  • 1
    It does find the double quotes. Did you step through the code with a debugger and see what the `tempt` value is when `x == 2`? – forgivenson Nov 11 '14 at 12:45
  • http://ideone.com/VWs6rl < "defsep used=34", check `defsepactivated[2]`, could be `false` P.s. you could use `char[] defseps = "]>\";%&".toCharArray();` if you want a simpler to read definition of your terminator characters – zapl Nov 11 '14 at 12:50
  • forgivenson be assured it wasn't lack of effort (2 days of sweat), just lack of brain and experience. But without your confirmation I wouldn't have got there. – Nigele2 Nov 11 '14 at 14:48
  • Many tx guys. zapl I'll try that more readable but any thoughts on return and carriage line feed in the char array? – Nigele2 Nov 11 '14 at 14:51

1 Answers1

0

Assuming you want to find the index of the first occurrence of any one of your "terminator" characters in a string, use a character class regex of your characters:

if (!str.matches(".*[\n\r\"#&'),;?\\]}].*")) {
    // handle not found
} else {
    int t = str.split("[\n\r\"#&'),;?\\]}]")[0].length - 1;
}

FYI, most characters lose their special regex meaning when in a character class and don't need escaping, but of course ] (which closes the character class) must be escaped.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Tx Bohemian. Yes first one. I'll have a look and see if I can form that regex as a string as sadly the terminators are not consistent so need to be selected from an options array. But no matter what learning a lot. Cheers – Nigele2 Nov 12 '14 at 07:49
  • If you don't know until runtime what the characters will be, you can safely form a regex using literal delimiters, but the code to do that would be more than your loop using indexOf(), which should work OK. – Bohemian Nov 12 '14 at 11:47