split string before hyphen - asp.net c#
I have a string :
10989898 - test1
or another example:
123178239182 - test2
I need the output like this:
In first case:
10989开发者_开发问答898
In second case:
123178239182
means the value before hyphen. How can I do that?
string result = theString.Substring(0, theString.IndexOf("-")).Trim();
You can use the String Split method:
string[] splitString = string.split('-');
string requiredString = splitString[0];
string s = "10989898 - test1";
string str = s.Substring( 0, s.IndexOf( "-" ) ).Trim();
http://jsfiddle.net/98Snm/2/
<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]", ""));
精彩评论