Link to home
Start Free TrialLog in
Avatar of banu111
banu111

asked on

How to extract some part of the string

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
Avatar of banu111
banu111

ASKER

Do they have Regex pattern for this
ASKER CERTIFIED SOLUTION
Avatar of Cong Minh Vo
Cong Minh Vo
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
string name = "MyComputer.Mytest.local";
int index = name.IndexOf('.');
name = name.Substring(index + 1, name.Length - index - 1);
Avatar of banu111

ASKER

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