Reputation: 13921
So I have a string like this:
string sampleString = "this - is a string - with hyphens - in it";
The thing to note here is that there are a random number of spaces to the left and to the right of the hyphens. The goal is to replace space in my string WITH a hyphen (hence the problem with hypens in the string). So the result I'm after should look like this:
"this-is-a-string-with-hyphens-in-it".
Currently I'm using:
sampleString.Trim().ToLower().Replace(" ", "-")
but this results in the following output:
"this---is-a-string------with-hyphens--------in-it"
Looking for the cleanest, most concise solution to this.
Thanks!
Upvotes: 3
Views: 5183
Reputation: 5239
Try using a System.Text.RegularExpressions.Regex
.
Just call :
Regex.Replace(sampleString, @"\s+-?\s*", "-");
Upvotes: 6
Reputation: 241601
Because everyone will propose a regex solution, I present you a non regex solution:
string s = "this - is a string - with hyphens - in it";
string[] groups = s.Split(
new[] { '-', ' ' },
StringSplitOptions.RemoveEmptyEntries
);
string t = String.Join("-", groups);
Upvotes: 9
Reputation: 4314
Regexes are your friend here. You can create a pattern, where all consecutive spaces/hyphens are a single match.
var hyphenizerRegex = new Regex(@"(?:\-|\s)+");
var result = hyphenizerRegex.Replace("a - b c -d-e", "-");
Upvotes: 0
Reputation: 29976
Regex:
var sampleString = "this - is a string - with hyphens - in it";
var trim = Regex.Replace(sampleString, @"\s*-\s*", "-" );
Upvotes: 0
Reputation: 60398
This looks like a job for regular expressions (or tokenization if you prefer that).
Using a regular expression you could slurp up all whitespace and hyphens and replace it with just one hyphen. This expression matches any number of spaces and hyphens:
[- ]+
Alternatively you can split the string up into tokens by whitespace, then recombine the string with hyphens between tokens unless the token itself is a hyphen. Pseudocode:
tokens = split(string," ")
for each token in tokens,
if token = "-", skip it
otherwise print "-" and the token
Upvotes: 1
Reputation: 88044
If you want all of the words AND existing hypens then another approach would be to split the string into an array breaking on spaces. Then rebuild the string, ignoring any spaces, while injecting hyphens were appropriate.
Upvotes: 0
Reputation: 74197
Try this:
private static readonly Regex rxInternaWhitespace = new Regex( @"\s+" ) ;
private static readonly Regex rxLeadingTrailingWhitespace = new Regex(@"(^\s+|\s+$)") ;
public static string Hyphenate( this string s )
{
s = rxInternalWhitespace.Replace( s , "-" ) ;
s = rxLeadingTrailingWhitespace.Replace( s , "" ) ;
return s ;
}
Upvotes: 0
Reputation: 48016
In a single line you can do this
Regex.Replace(sampleString, @"\s+", " ").Replace (" ", "-");
Upvotes: 0