如何从字符串中获取子字符串

时间:2014-01-09 10:40:01

标签: c#

如果字符串包含以下字符“特定于连接的DNS后缀。:”后跟任何字符串,我该如何获取子字符串?

前:

Windows IP Configuration


Wireless LAN adapter Wireless Network Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Ethernet adapter Local Area Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : abc.abc.com
   Link-local IPv6 Address . . . . . : fe80::8ca3:bc6c:d958:f1f5%13
   IPv4 Address. . . . . . . . . . . : 10.96.72.154
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.96.72.1

Ethernet adapter Local Area Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : abc.abc.com

Tunnel adapter isatap.asia.jci.com:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : abc.abc.com

Tunnel adapter Local Area Connection* 12:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter isatap.{DB074F38-9E68-4ABE-AF31-D3750FE10DE1}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter isatap.{01405796-6937-431A-B61D-DBC785F4F56B}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

我只想要特定于连接的DNS后缀。 :abc.abc.com 来自孔串的字符串

3 个答案:

答案 0 :(得分:4)

所以文本包含多行,你想要整行吗?

string result = allLines.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
  .FirstOrDefault(l=> l.TrimStart().StartsWith("Connection-specific DNS Suffix . :"));

如果找不到它,则为null

if(result != null) result = result.Trim(); 

如果它不包含多行,并且您想在字符串的中间找到它:

string result = null;
string textToFind = "Connection-specific DNS Suffix  . :";
int startIndex = text.IndexOf(textToFind);
if (startIndex >= 0)
{ 
    string behind = text.Substring(startIndex + textToFind.Length).TrimStart();
    int endIndex = behind.IndexOf(" ");
    if (endIndex >= 0)
        behind = behind.Substring(0, endIndex).TrimEnd();
    result = string.Format("{0} {1}", textToFind, behind);
}

请注意,默认情况下,字符串比较在.NET中区分大小写。如果您想忽略这种情况(以便Connectionconnection被视为相同),您可以使用StringComparison的重载,例如:{ / p>

int startIndex = text.IndexOf(textToFind, StringComparison.OrdinalIgnoreCase);

答案 1 :(得分:0)

int startIndex = s.IndexOf("Connection-specific DNS Suffix", StringComparison.CurrentCultureIgnoreCase);
string substring  = s.Substring(startIndex, s.IndexOf(Environment.NewLine, startIndex) - startIndex);

答案 2 :(得分:0)

它适用于所有情况

 string your_string = "tomany_words_Connection-specific DNS Suffix . :abcd.comLinkbfdbfdb";
        string check_string="Connection-specific DNS Suffix . :";
        int index;
        int last_index;
        string final;
        if (true == your_string.Contains(check_string))
        {
            index=your_string.IndexOf(check_string);
            last_index = your_string.IndexOf("Link");

            final = your_string.Substring(index + check_string.Length, last_index - (index + check_string.Length));              
            Console.WriteLine(final);
        }