How do you split something with an apostrophe?

I have this sample input:

cup adam’s apple mayo

Basically, I want to get the “cup” text using “adam’s apple” as delimeter, and so I do this:

Split(textData.ToLower,“adam’s apple”)(0).ToString

I however get this error:

Index was outside the bounds of the array

Is there any way how to do this? Btw, “cup” could be a long string of text and not just a single word.

Hi

You can use regex

Try this:

System.Text.RegularExpressions.Regex.Match(StrInput, “[\s\S]+(?=adam’s apple)”).ToString

Cheers

Steve

Hi,
this it works: yourText.Split(“adam’s apple”)

Hi @LagunaAJ

Can you try the below

Input="cup adam’s apple mayo"

Input.ToLower.Split({"adam’s apple"}, StringSplitOptions.None)(0).ToString

Cheers!!

Hi,

I think the above expression doesn’t throw this exception because Split method returns 1 item at least and index 0 always exists. Did you get this exception using index 0? There might be something wrong except this expression.

FYI, your expression seems to works well.

image

Regards,

Hi @LagunaAJ

Try this

Assign-> textData = "cup adam’s apple mayo"

Assign-> delimiter = "adam’s apple"

Assign-> result = parts(0).Trim()

Regards

Hi @Yoichi, I tried again after I saw your comment, and I actually used (1) in my case, since I was extracting the text after the delimiter.

When using (0), it’s returning all text as if “adam’s apple” doesn’t exist. When I use (1) however, that’s when it throws the error.

I provided the wrong example here but I’ll try to work out with what the people has provided so far, so that it captures the text after, and not before.

Hi @lrtetala I tried this out with the following:
textData.ToLower.Split({“adam’s apple”},StringSplitOptions.None)(1).ToString

but it didn’t work. I was actually getting the text after the delimiter though, so it’s a (1) and not (0). I made the wrong example in the post, but I assumed it would work if I just changed (0) to (1).

I believe I found the issue. The apostrophe used was not the normal one, but the ’ one, which is why it’s not detecting as delimiter.

I’ll be closing out this one now, and have to deal with an entirely different issue. Thanks everyone!

Hi,

All right. Probably there is a slight diffrence between your input text and separator string, for example white space and/or apostrophe.
If you want to investigate your string in details, the following expression will help you.

String.Join(",",textData.ToLower.Select(Function(c) AscW(c)))

This outputs character code in the string and we can know exact character used in the string.

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.