My String will be Srting itemno = 899999-001 but sometimes it may be 899999-001-999
I just need the first dash number of either(-001)
The following will get me -001 and -001-999
If I use lastIndexOf and there is just -001 it will return empty.
hh - I understood it as he doesn't want the tailing bit if it is present, so I though this:
But now that I re-read it, you are right, only the part at the end if there was 2 sections, or in the middle of there was 3 sections, so something like this:
Edit: I didn't include the dash character in the output- but clearly, just move the bracket on the first capture group to grab what is needed
You guys got so many potential solutions, while I'm struggling even to understand what OP wants.
Here is what we have:
Steve Dyke wrote:I just need the first dash number of either(-001)
The following will get me -001 and -001-999
If I use lastIndexOf and there is just -001 it will return empty.
He wants the three digits after the first hyphen. He also wants the first hyphen along with it but we're ignoring that here because it is a constant and can easily be prepended regardless of the parsing technique. If there's a second hyphen he wants to ignore that and anything that comes after.
I went with the third option you suggested Carey. It works great and as expected.
All were great answers and thanks for everyone's input.
This is a great site and very helpful for those of us that are not so keen as we wish we were.
Even I was surprised by something. I knew of the split() method but it didn't occur to me to use it in this context. So, there you go. This is what makes this site so valuable.