2014-01-09 3 views
1

Как получить строку подстроки, если строка содержит следующие символы «Суффикс DNS для подключения:.», За которым следует любая строка?Как получить строку подстроки из строки

Ex:

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 строка из отверстия строки

+0

Является ли адрес DNS завершен символом новой строки? –

+0

yes DNS-адрес, прерванный символом новой строки – Sumit

ответ

0
int startIndex = s.IndexOf("Connection-specific DNS Suffix", StringComparison.CurrentCultureIgnoreCase); 
string substring = s.Substring(startIndex, s.IndexOf(Environment.NewLine, startIndex) - startIndex); 
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 по умолчанию. Если вы хотите, чтобы игнорировать регистр (так что Connection и connection обрабатываются одинаково), вы можете использовать перегрузку String.IndexOf, которая принимает StringComparison, например:

int startIndex = text.IndexOf(textToFind, StringComparison.OrdinalIgnoreCase); 
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); 
     } 
Смежные вопросы