3

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!

8 Answers 8

9

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);        
Sign up to request clarification or add additional context in comments.

2 Comments

If it's a problem that is just as easy to solve using regular expressions as it is without, going without lends itself to a simpler, easier-to-understand solution. +1.
@AndyPerfect: Yes. Regular expressions are soooooooooooooooo overused (so are repeated letters for the purpose of emphasis).
6

Try using a System.Text.RegularExpressions.Regex.

Just call :

Regex.Replace(sampleString, @"\s+-?\s*", "-");

Comments

1

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

Comments

0

In a single line you can do this

Regex.Replace(sampleString, @"\s+", " ").Replace (" ", "-");

Comments

0

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 ;
}

Comments

0

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.

Comments

0

Regex:

var sampleString = "this - is a string   - with hyphens  -     in it";
var trim = Regex.Replace(sampleString, @"\s*-\s*", "-" );

Comments

0

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", "-");

1 Comment

Wanted to make it as explicit as possible. Scott didn't seem to know regexes so I wanted to make it clear, that it is going to be a whitespace or a hyphen. (After he took a short look at some regex cheat sheet)

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.