1

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" 
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kevin himch
  • 287
  • 3
  • 18
  • Possible duplicate of [Get Substring - everything before certain char](https://stackoverflow.com/questions/1857513/get-substring-everything-before-certain-char) – Fredou Dec 17 '17 at 14:28

2 Answers2

4

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" 
Backs
  • 24,430
  • 5
  • 58
  • 85
2

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);
styx
  • 1,852
  • 1
  • 11
  • 22