Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Split the string based on Decimal point

Posted on 2011-02-21
3
Medium Priority
?
1,237 Views
Last Modified: 2012-05-11
Hi,

I have ths code,i want to split the string based on decimal point

Eg: Jack.Smith

i want to split the sting and get only smith

my code

String = " Jack.Smith"

 if (username.Contains("."))
           
         {            
           // username = username.Replace(".", " ");
              string updateuser=username.Substring(0,username.IndexOf("."));
               Console.WriteLine(updateuser);
          }
0
Comment
Question by:Sha1395
3 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 1000 total points
ID: 34948855
String has a split function.

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

string[] splitString = username.Split(new char[] { '.' });

Open in new window


and if you want to get the last entry after the split

string surname = splitString[splitString.Length-1];

Open in new window


which is fine as you've already tested that the string contains at least one '.', so the split will contain at least 2 elements.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34948862
Please try the following:

string username = " Jack.Smith";

if (username.Contains("."))
{            
	string[] names = username.Split('.');
	
	Console.WriteLine(names[1].Trim());
}

Open in new window


I hope this helps.
0
 
LVL 2

Expert Comment

by:chTeja
ID: 34948877
Hi

Please try the following code.
string str = "Jack.Smith";
            if (str.Contains('.'))
            {
                // When we do split on the string, it will internally store as a string arry. to print all the values
               // i am doing the foreach loop
                foreach (var item in str.Split('.'))
                {
                    Console.WriteLine(item.ToString());
                }
            }

Open in new window

Let me know your feed back..

Thanks
Sri Narayana Rao.D
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question