Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting up a string, based on predicate

Tags:

c#

linq

I want to split up a string, based on a predicate. As an example:

"ImageSizeTest" should become "Image Size Test"

Note: Uppercased character is the predicate

Of course I could write a simple loop, going through the string, check for uppercased characters (the predicate) and build the new string. However I want this to be a bit more general, splitting up based on any predicate. Still not very hard to implement, but I was wondering if there is an elegant way to do this using Linq.

Reference:

  1. Splitting a String with two criteria
like image 886
Maurits Rijk Avatar asked Feb 15 '10 10:02

Maurits Rijk


People also ask

How do you split part of a string?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do I split a string into multiple characters?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.

How do you split a list by delimiter in Python?

Split by delimiter: split()Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.


1 Answers

I take it that you don't want to split it into an array, but rather introduce spaces to a string? If so, you could use a regular expression to replace each upper case character with [space] character. You'd need to trim off the leading space though.

Sorry, to answer the full question, you could make it more generic by passing in the regular expression to match and the string to replace the matches with.

Looking at http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx, could you not consider the MatchEvaluator to be your predicate?

like image 132
Paul Manzotti Avatar answered Oct 03 '22 06:10

Paul Manzotti