Reputation: 6540
I have a string :
10989898 - test1
or another example:
123178239182 - test2
I need the output like this:
In first case:
10989898
In second case:
123178239182
means the value before hyphen. How can I do that?
Upvotes: 8
Views: 20538
Reputation: 116
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
});
function replaceFunc() {
var s = document.getElementById("foo1").value;
alert(s.replace(/[^0-9]/g, ""));
}
</script>
</head>
<body>
<input type="button" value="replace non-numeric" onclick="replaceFunc()" />
<input type="text" id="foo1" />
</body>
</html>
Update
Regex reg = new Regex("[^0-9]", RegexOptions.Singleline);
Console.WriteLine(Regex.Replace("123 -test 456", "[^0-9]", ""));
Upvotes: 0
Reputation: 14998
string result = theString.Substring(0, theString.IndexOf("-")).Trim();
Upvotes: 17
Reputation: 11090
You can use the String Split method:
string[] splitString = string.split('-');
string requiredString = splitString[0];
Upvotes: 5
Reputation: 74156
string s = "10989898 - test1";
string str = s.Substring( 0, s.IndexOf( "-" ) ).Trim();
Upvotes: 3