-1

I have to find the occurrence of * in a String and based on the position of the * in the string certain operations have to be performed.

if(* found in the beginning of the String) {
 do this
}
if(* found in the middle of the String) {
 do this
}
if(* found at the end of the String) {
 do this
} 

I used matcher.find() option but it is not giving the desired result.

Leo
  • 5,017
  • 6
  • 32
  • 55
  • 9
    Why haven't you use `indexOf()`? – TheLostMind Dec 21 '15 at 16:20
  • 2
    Possible duplicate of [Java: method to get position of a match in a String?](http://stackoverflow.com/questions/2615749/java-method-to-get-position-of-a-match-in-a-string) – Fundhor Dec 21 '15 at 16:21
  • "it is not giving the desired result" What result is it giving you? For what input? What is the desired result? – Andy Turner Dec 21 '15 at 16:22
  • Question already answered. Just for information you should consider using "else if" . http://stackoverflow.com/questions/9169249/why-we-use-if-else-if-instead-of-multiple-if-block-if-the-body-is-a-return-stat – Fundhor Dec 21 '15 at 16:25

1 Answers1

7

Use String.indexOf:

int pos = str.indexOf('*');
if (pos == 0) {
  // Found at beginning.
} else if (pos == str.length() - 1) {
  // Found at end.
} else if (pos > 0) {
  // Found in middle.
}

An alternative would be to use startsWith/endsWith/contains:

if (str.startsWith('*')) {
  // Found at beginning.
} else if (str.endsWith('*')) {
  // Found at end.
} else if (str.contains('*')) {
  // Found in middle.
}

which might be marginally more efficient, since it avoids having to check the entire string in the case that it ends with *. However, readability of the code should be the primary concern in selecting between these two, since the performance difference would be negligible in many cases.

And, of course, you don't get the actual position of the * if you use the latter approach. It depends upon what you are really trying to do as to whether that matters.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • startsWith("") endsWith("") may also be useful. – Fundhor Dec 21 '15 at 16:23
  • 1
    @Fundhor well yes, but you'd also need `contains` in that list too if you are going to mention those. Feel free to add an answer of your own, it's a valid approach. – Andy Turner Dec 21 '15 at 16:23
  • nope, your answer is good, no point to add my own. It's just that it can be useful in different circonstances (for information). – Fundhor Dec 21 '15 at 16:26
  • @Fundhor I've added that approach too. – Andy Turner Dec 21 '15 at 16:38
  • I'm quite happy that this answer doesn't try to misuse regex, like OP wanted :). – Tom Dec 21 '15 at 16:43
  • 1
    @Tom I find that the best approach to "how to use regexes to..." questions is usually "don't". – Andy Turner Dec 21 '15 at 16:45
  • Also I have a hard question about regex, May you please take a look at [it](http://stackoverflow.com/questions/34406286/how-to-prevent-of-re-replacing-by-second-regex) ? – stack Dec 22 '15 at 01:31