如何拆分字符串 [英] how to split string
问题描述
我有一个字符串 我的号码是--------------------------- -------------- 890现在我想要子串 我的号码只有
有任何方法......
----------表示空格
看看这里:
如何:使用字符串方法搜索字符串(C#编程指南) [ ^ ]
代码:在字符串中搜索(Visual C#) [ ^ ]
由于您已经知道您正在搜索我的号码是
,你不需要o知道它是什么。
但是因为你努力写了一个问题,我想你实际上是在寻找空间之前的任何东西。所以你可以试试这个:string input = 我的号码是----------------------------------------- 890\" 跨度>;
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( [0-9]);
System.Text.RegularExpressions.Match match = regex.Match(input);
string key = input.SubString( 0 ,match.Index);正则表达式检查空格后跟另一个空格或数字。所以为了这个工作,那里
- 必须至少在key
和电话号码之间的一个空格
- 不能是key
中任何数字的起始单词。
或者,这个怎么样:regex = new 正则表达式( [0 -9] {3,...}跨度>);
匹配匹配= regex.Match(输入);
int numberIndex = match.Index;
string key = input.SubString( 0 ,numberIndex);
key = key.Trim()
啊,别忘了:string input = 我的号码是------- ---------------------------------- 890\" 跨度>;
string key = input.Trim( new Char [] {' ',' 0',' 1',' 2',' 3',' 4',' 5',' 6',< span class =code-string>' 7',' 8',' 9'});
您好,
使用如下:
string original = 我的电话号码是123456789;
int pos = original.IndexOf( 1\" 跨度>);
string strNew = original.Substring( 0 ,pos).Trim();
通过这种方式,你可以得到所需的结果。
谢谢
I have one string that is " my number is -----------------------------------------890"now i want sub string my number is that only
have any methods...
----------means spaces
Have a look here:
How to: Search Strings Using String Methods (C# Programming Guide)[^]
Code: Searching Within a String (Visual C#)[^]
Since you already know that you're searchning for"My number is"
, you don't need to know what it is.
But because you made the effort to write a question, I guess you're actually looking for whatever comes before the spaces. So you could try this:string input = "my number is -----------------------------------------890"; System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(" [ 0-9]"); System.Text.RegularExpressions.Match match = regex.Match(input); string key = input.SubString(0, match.Index);The regular expression checks for a space followed by another space or number. So for this to work, there
- must be at least one space betweenkey
and the phone number
- must not be any numbers starting words withinkey
.
Or, what about this one:regex = new Regex("[0-9]{3,}"); Match match = regex.Match(input); int numberIndex = match.Index; string key = input.SubString(0, numberIndex); key = key.Trim()
Ah, forget it all:string input = "my number is -----------------------------------------890"; string key = input.Trim(new Char[]{' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
Hi,
Use like the following:
string original = "my phone number is 123456789"; int pos = original.IndexOf("1"); string strNew = original.Substring(0, pos).Trim();
In this way ou can able to get the required result.
Thanks
这篇关于如何拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!