C# 当要查找的字符串具有不同的空格时提取子字符串

C# 当要查找的字符串具有不同的空格时提取子字符串,c#,string,vb.net,substring,indexof,C#,String,Vb.net,Substring,Indexof,我有一根像下面这样的线 传真:666-111-2222电话#:200100200 我想找到电话号码。但问题是,Phone和after#之后的空格数量可能会因提取数据的不同字符串而异。另外,不建议编写复杂函数,因为我有一个大数据集要从中提取数据 我尝试了下面的代码,它给了我正确的起始索引,有n个空格。但我找不到之后的位置:从那 System.Globalization.CultureInfo.InvariantCulture.CompareInfo.IndexOf(FullString,"Phon

我有一根像下面这样的线

传真:666-111-2222电话#:200100200

我想找到电话号码。但问题是,Phone和after#之后的空格数量可能会因提取数据的不同字符串而异。另外,不建议编写复杂函数,因为我有一个大数据集要从中提取数据

我尝试了下面的代码,它给了我正确的起始索引,有n个空格。但我找不到之后的位置:从那

System.Globalization.CultureInfo.InvariantCulture.CompareInfo.IndexOf(FullString,"Phone#:",System.Globalization.CompareOptions.IgnoreSymbols)
我想你需要一个C#答案

我会使用正则表达式,但如果您坚持使用
IndexOf
,您可以:

string fullString=“传真:666-111-2222电话#:200100200”;
int phonePos=fullString.IndexOf(“电话”);
int hashPos=fullString.IndexOf(“#”,phonePos+“Phone.Length”);
int colonPos=fullString.IndexOf(“:”,hashPos+1);
这样你就有了冒号的绝对位置,不管有多少个空格。 注意,我使用了
String.IndexOf
。没有理由像你那样从CompareInfo中挖掘它。 还要注意,我使用的重载包含一个额外的参数,即开始索引。

我假设您需要一个C#答案

我会使用正则表达式,但如果您坚持使用
IndexOf
,您可以:

string fullString=“传真:666-111-2222电话#:200100200”;
int phonePos=fullString.IndexOf(“电话”);
int hashPos=fullString.IndexOf(“#”,phonePos+“Phone.Length”);
int colonPos=fullString.IndexOf(“:”,hashPos+1);
这样你就有了冒号的绝对位置,不管有多少个空格。 注意,我使用了
String.IndexOf
。没有理由像你那样从CompareInfo中挖掘它。
还请注意,我使用的重载包含一个额外的参数,即开始索引。

在Phone和#之间,也在#和之间有一个空格:。带有单个参数的子字符串将返回一个从该索引到输入字符串末尾的字符串。修剪将删除任何一侧的空白

Private Function GetPhone(input As String) As String
    Dim i = input.IndexOf("Phone")
    Dim s = input.Substring(i)
    Dim splits = s.Split(":"c)
    Return splits(1).Trim
End Function
我运行该函数10000次,耗时5毫秒

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim s = "Fax: 666-111-2222 Phone # : 200100200"
    Dim Phone As String = ""
    Dim sw As New Stopwatch
    sw.Start()
    For i = 0 To 10_000
        Phone = GetPhone(s)
    Next
    sw.Stop()
    Debug.Print(sw.ElapsedMilliseconds.ToString)
    MessageBox.Show(Phone)
End Sub

在Phone和#之间,以及#和:。带有单个参数的子字符串将返回一个从该索引到输入字符串末尾的字符串。修剪将删除任何一侧的空白

Private Function GetPhone(input As String) As String
    Dim i = input.IndexOf("Phone")
    Dim s = input.Substring(i)
    Dim splits = s.Split(":"c)
    Return splits(1).Trim
End Function
我运行该函数10000次,耗时5毫秒

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim s = "Fax: 666-111-2222 Phone # : 200100200"
    Dim Phone As String = ""
    Dim sw As New Stopwatch
    sw.Start()
    For i = 0 To 10_000
        Phone = GetPhone(s)
    Next
    sw.Stop()
    Debug.Print(sw.ElapsedMilliseconds.ToString)
    MessageBox.Show(Phone)
End Sub

这显然是正则表达式的工作

String toMatch = "Fax : 666-111-2222 Phone # : 200100200";
Regex matchPhone = new Regex("\\bPhone\\s*#\\s*:\\s*");
MatchCollection matches = matchPhone.Matches(toMatch);
foreach (Match match in matches)
{
    Int32 position = match.Index + match.Length;
    // do whatever you want with the result here
}
在代码中,反斜杠加倍,但实际的正则表达式是:

\b电话\s*\s*:\s*

  • \b
    表示单词的边界、意思、单词的开头或结尾。这也可以防止类似“扩音器”的东西匹配
  • \s
    表示任何类型的空白。这将匹配空格、制表符和换行符
  • *
    表示零次或多次重复,也就是说,不管空格是否存在,或者是否有100个空格,都会匹配
请注意,这将仅为您提供给定字符串中找到的所有电话号码的开头索引。您没有指定是否有任何特定的方法来检测电话号码的结尾,或者甚至没有指定它们的任何特定预期格式,因此不包括在内。如果您想要这样做,并且您不知道这个电话号码后面可能跟什么,请查看正则表达式字符组和匹配的特定数字内容,并使用捕获组从匹配的内容中提取它

如果整个字符串中只需要一个匹配项,则可以使用

String toMatch = "Fax : 666-111-2222 Phone # : 200100200";
Regex matchPhone = new Regex("\\bPhone\\s*#\\s*:\\s*");
Match match = matchPhone.Match(toMatch);
Int32 position = match.Index + match.Length;

这显然是正则表达式的工作

String toMatch = "Fax : 666-111-2222 Phone # : 200100200";
Regex matchPhone = new Regex("\\bPhone\\s*#\\s*:\\s*");
MatchCollection matches = matchPhone.Matches(toMatch);
foreach (Match match in matches)
{
    Int32 position = match.Index + match.Length;
    // do whatever you want with the result here
}
在代码中,反斜杠加倍,但实际的正则表达式是:

\b电话\s*\s*:\s*

  • \b
    表示单词的边界、意思、单词的开头或结尾。这也可以防止类似“扩音器”的东西匹配
  • \s
    表示任何类型的空白。这将匹配空格、制表符和换行符
  • *
    表示零次或多次重复,也就是说,不管空格是否存在,或者是否有100个空格,都会匹配
请注意,这将仅为您提供给定字符串中找到的所有电话号码的开头索引。您没有指定是否有任何特定的方法来检测电话号码的结尾,或者甚至没有指定它们的任何特定预期格式,因此不包括在内。如果您想要这样做,并且您不知道这个电话号码后面可能跟什么,请查看正则表达式字符组和匹配的特定数字内容,并使用捕获组从匹配的内容中提取它

如果整个字符串中只需要一个匹配项,则可以使用

String toMatch = "Fax : 666-111-2222 Phone # : 200100200";
Regex matchPhone = new Regex("\\bPhone\\s*#\\s*:\\s*");
Match match = matchPhone.Match(toMatch);
Int32 position = match.Index + match.Length;

如果您可以依赖于格式,那么这非常简单。 只需清除所有空格(
.Replace(“,string.Empty)
)的字符串,然后在电话号码开始后的字符上拆分,例如“#:”:


如果您可以依赖于格式,那么这非常简单。 只需清除所有空格(
.Replace(“,string.Empty)
)的字符串,然后在电话号码开始后的字符上拆分,例如“#:”:


我想你应该你的正则表达式:

Regex rxPhone = new Regex(@"Phone\s*#\s*:\s*(\d+)");
Match match = rxPhone.Match(stringToMatch);
if (match.Success) //if the phone does not always exits
{
    string strPhoneNumber = match.Groups[1];
    int intPhoneNumber = int.Parse(match.Groups[1]);
    int position = match.Groups[1].Index
    //just pick the one you need
}

我想你应该你的正则表达式:

Regex rxPhone = new Regex(@"Phone\s*#\s*:\s*(\d+)");
Match match = rxPhone.Match(stringToMatch);
if (match.Success) //if the phone does not always exits
{
    string strPhoneNumber = match.Groups[1];
    int intPhoneNumber = int.Parse(match.Groups[1]);
    int position = match.Groups[1].Index
    //just pick the one you need
}

我将有数百个:'string@PraveenVenu-您需要在问题中明确指定您的输入数据。请不要将信息作为注释添加到答案中。如果您的问题不清楚,请修复该问题。不管字符串中有多少冒号。代码会在#Phone之后找到第一个冒号。我将在string@PraveenVenu-您需要在问题中明确指定您的输入数据。请不要添加通知