Kevin himch
Kevin himch

Reputation: 297

remove all characters after X character

Please check variable "mystr" value where a "-" sign between two part of numbers. I want to find "-" then remove all character after that then I want find same "-" and remove all Character from first to till that. I know it's simple but not getting exact solution on c# due to I am new.

public void test()
    {
        string mystr = "1.30-50.50";

        //first output I want is-  "1.30" 
        //second output I want is-  "50.50" 
    }

Upvotes: 1

Views: 120

Answers (2)

styx
styx

Reputation: 1925

you can also String.IndexOf method

string mystr = "1.30-50.50";
int indexOfDash = mystr.IndexOf('-');
string firsResult = mystr.Substring(0, indexOfDash);
string secondResult = mystr.Substring(indexOfDash + 1, mystr.Length - indexOfDash - 1);

Upvotes: 2

Backs
Backs

Reputation: 24913

Use string.Split method:

var mystr = "1.30-50.50";
var result = mystr.Split('-');
var a = result[0]; //"1.30" 
var b = result[1]; //"50.50" 

Upvotes: 4

Related Questions