Something like: How to split a String with some separator but not removing that separator in Java?
I need to take "Hello World" and get ["Hello", "," World "]
You can use regex, although this is probably brute force:
StringCollection resultList = new StringCollection(); Regex regexObj = new Regex(@"(?:\b\w+\b|\s)"); Match matchResult = regexObj.Match(subjectString); while (matchResult.Success) { resultList.Add(matchResult.Value); matchResult = matchResult.NextMatch(); }
You can use Regex.Split() . If you attach a template to capturing parentheses, it will also be included in the result:
Regex.Split()
Regex.Split("Hello World", "( )")
gives you exactly what you wanted.
If you split only at the border of the word, you will get something very close to what you are asking.
string[] arr = Regex.Split("A quick brown fox.", "\\b");
arr [] = {"," A ",", "quick", "," brown ",", "fox", "." }
Source: https://habr.com/ru/post/901921/More articles:Goal C: My custom -init method does not receive a call - objective-cHTML5 Canvas filltext and font-face - javascriptWill the memory be used by MarshalAs (UnmanagedType.LPWStr)? - c #The longest common substring for more than two lines in PowerShell? - powershellassertEquals and assertTrue give different results for the same variables - phphttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/901922/using-custom-xml-namespaces-to-reference-external-dlls-in-kaxaml&usg=ALkJrhgANyIUjeR_fFhhxzJxmO3acI0HfwHow to write DD-WRT C application? - cPHP Fatal error: Class 'AMQPConnection' not found - phpHTTPClient 4.x Connection reuse does not occur - javajQuery Date Picker does not show the original text value - jqueryAll Articles