1

How do I get sub string if a string contains the following characters "Connection-specific DNS Suffix . :" followed by any string?

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  . :

i want only Connection-specific DNS Suffix . : abc.abc.com string from hole string

2
  • Is the DNS address terminated by a newline? Commented Jan 9, 2014 at 10:41
  • yes DNS address terminated by a newline Commented Jan 9, 2014 at 10:43

3 Answers 3

4

So the text contains multiple lines and you want the whole line?

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

it's null if it was not found.

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

If it doesn't contain multiple lines and you want to find it in the middle of the string:

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);
}

Note that string comparisons are case sensitive in .NET by default. If you want to ignore the case (so that Connection and connection are treated the same) you can use the overload of String.IndexOf that takes a StringComparison, for example:

int startIndex = text.IndexOf(textToFind, StringComparison.OrdinalIgnoreCase);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

it will work in all the situations

 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);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.