0

How can I split a string based on a string and have the resulting array contain the separators as well?

Example:

If my string = "Hello how are you.Are you fine?.How old are you?"

And I want to split based on string "you", then result I want is an array with items { "Hello how are", "you", ".Are", "you", "fine?.How old are", "you", "?" }.

How can I get a result like this? I tried String.Split and

string[] substrings = Regex.Split(source, stringSeparators);

But both are giving the result array without the occurrence of you in it.

Also, I want to split only on the whole word you. I don't want to split if you is a part of some other words. For example, in the case Hello you are so young, I want the result as { "Hello", "you", "so young" }. I don't want to split the word young to { "you", "ng" }.

2

4 Answers 4

4

You can put the seperator into a match group, then it will be part of the result array:

string[] substrings = System.Text.RegularExpressions.Regex.Split(source, "(you)");

Output would be :

"Hello how are ", 
"you" ,
".Are ",
"you",
" fine?.How old are ",
"you",
"?"

Update regarding your additional question: Use word-boundaries around the keyword:

Split(source, "\\b(you)\\b");
Sign up to request clarification or add additional context in comments.

6 Comments

Also i want to split only for the word "you". I dont want to split ifyou is a part of some other words . Example Hello you are so young . In this case i want result as "Hello" "you" "so young" . Dont want to split the word young to "you" and "ng"
ie i want to split either by "you" or " you " or " you" or "you " . [ without space , with space , with space in left / right etc]
string[] substrings = System.Text.RegularExpressions.Regex.Split("Hello how are you.Are you fine?.How old are you?.Hello how old are you?.Hello you are so young", "(you)"); int s = substrings.Length; substrings = System.Text.RegularExpressions.Regex.Split("Hello how are you.Are you fine?.How old are you?.Hello how old are you?.Hello you are so young", "\b(you)\b"); s = substrings.Length; i tried the suggestion , but first one gives some result. Second one not returning anything
@JMat My bad, you ofc. need to escape the \b in the pattern, see the edit.
Yes Its perfect now. Just one more case to handle Case sensitiveness? How can i achieve that . Now if You is present in string it skips them since Y is in caps
|
1
\b(you)\b

Split by this and you have your result.

1 Comment

string[] substrings = System.Text.RegularExpressions.Regex.Split("Hello how are you.Are you fine?.How old are you?.Hello how old are you?.Hello you are so young", "(you)"); int s = substrings.Length; substrings = System.Text.RegularExpressions.Regex.Split("Hello how are you.Are you fine?.How old are you?.Hello how old are you?.Hello you are so young", "\b(you)\b"); s = substrings.Length; i tried the suggestion , but first one gives some result. Second one not returning anything
0

regex replace :

(you) with |\1|

now you will have a string like this :

Hello how are |you|.Are |you| fine?.How old are |you|?

now you can simply split on |

Hope that helps

Comments

0

string[] substrings = System.Text.RegularExpressions.Regex.Split(source,"\s* you\s*");

This should work. Below is the output.

"Hello how are"

".Are"

"fine?.How old are"

"?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.