How to extract some part of the string

banu111
banu111 used Ask the Experts™
on
Hi friends I have a full computer name MyComputer.Mytest.local stored in the database
i want just the domain like Mytest.local .
How can extract it from the string like

string name=MyComputer.Mytest.local;
to Mytest.local;
or is there pattern to recognize domain name from full computer name
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Do they have Regex pattern for this
string name = "MyComputer.Mytest.local";
name = name.Substring(name.IndexOf('.') + 1, name.Length - name.IndexOf('.') - 1);
//name = Mytest.local
anarki_jimbelSenior Developer

Commented:
One way to handle this is to split the total name into substrings using "." as a delimiter:

            string name = "MyComputer.Mytest.local";
            string[] subnames = name.Split(new char[] { '.' });
            foreach (string s in subnames)
            {
                Console.Out.WriteLine(s);
            }

Open in new window


Output:

MyComputer
Mytest
local


You may use array elements to build any string or use in any other way you want.
Anuradha GoliSystems Development / Support Specialist

Commented:
string name = "MyComputer.Mytest.local";
int index = name.IndexOf('.');
name = name.Substring(index + 1, name.Length - index - 1);

Author

Commented:
if  name=MyComputer.Mytest.local  or name=Mytest.local. If I want to test the string if it contains more than one (.) then  only extract the string. I got the answer for extracting the string but how to check the (.) condition

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial