0

I have written a funny looking code that if the first character of string is blank, it will replace that blank space with a "0" and it works but like I said it is funny looking. Is there a nicer way of writing the same thing?

    char ch = value[0];
    if (ch == ' ')
    {
        value = value.Trim();
        value = "0" + value;
    }
    return value;
  • See [What's the best string concatenation method using C#?](http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c). – CodeCaster Nov 18 '13 at 13:23
  • return value[0] == ' ' ? "0" + value.Trim() : value; – Dave Nov 18 '13 at 13:24

6 Answers6

8

It's as simple as:

if (value.Length > 0 && value[0] == ' ')
    value = '0' + value.Substring(1);
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 1
    Actually, I'm not checking `value` for nullness - the OP didn't either, but if that's important then add a `value != null &&` to the `if` – Matthew Watson Nov 18 '13 at 13:41
1

Whatever you want to achieve, this one is shorter:

if(value.StartsWith(" "))
{
  value = string.Format("0{0}", value.Trim());
}
MatthiasG
  • 4,434
  • 3
  • 27
  • 47
  • Almost. Will erroneously convert `__foo_` to `0foo` instead of `0_foo_`. – Rotem Nov 18 '13 at 13:33
  • I just converted the code of DevWannaBe, not the problem explained. It was difficult to know what was the intension of the question. – MatthiasG Nov 18 '13 at 13:37
  • 1
    you're right, the description doesn't match the OP's sample code. I just thought the caveat is worth mentioning. – Rotem Nov 18 '13 at 13:42
1
if (value.StartsWith(" "))
{
    value = value.TrimStart(' ');
    value = value.PadLeft(value.Length + 1, '0');
}

Assuming value is not a null reference. For more information see this.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
1

You can use a regular expression to match the spaces at the beginning of the string and replace with a zero character:

value = Regex.Replace(value, "^ +", "0");

If you only want to replace a single space at the beginning of the string, if there happen to be more than one:

value = Regex.Replace(value, "^ ", "0");

Note: This differs from your original implementation in two ways: It will only remove actual spaces from the beginning of the string, not any white space characters as the Trim method does, and it will only remove spaces from the beginning of the string, not also the end of the string, as the Trim method does. I assume that those details were not actually the intention of the code.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

I would use Trim() and PadLeft():

        string value = " 5678";
        value = value.Trim().PadLeft(5, '0');

If you had "1", or " 1", or any combination of "1" and spaces, it would end up being "00001".

You just change the 5 to however wide you need your field to be.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Try this

 string value = " String with space";
 var firstChar = (byte)value.First();
 if (firstChar == ' ')
 {
    var regex = new Regex(Regex.Escape(" "));
    value = regex.Replace(value, "0", 1);
 }
 return value;
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
  • Is this really better than the original code? Maybe more complex or sophisticated, but better? – gehho Nov 18 '13 at 13:33