jeff ninney
jeff ninney

Reputation: 25

Split null or space on the first of a string?

I'm getting a string " abc df fd"; I want to split a space or null of string. As result is "abc df fd" That such I want;

 private string _senselist;
    public string senselist
    {
        get
        {
            return _senselist;
        }
        set
        {
            _senselist = value.Replace("\t", "").Replace(" "," ").Split(,1);
        }
    }

Upvotes: 2

Views: 192

Answers (1)

adricadar
adricadar

Reputation: 10219

To remove spaces from the begining and the ending of a string, you can use Trim() method.

string data = "    abc df fd";
string trimed = data.Trim(); // "abc df fd"

On your code add Trim at the end, instead of Split

_senselist = value.Replace("\t", "").Replace(" "," ").Trim();

As @andrei-rînea recommended you can also check TrimStart(' ') and TrimEnd(' ').

Upvotes: 5

Related Questions