Solved

Split the string based on Decimal point

Posted on 2011-02-21
3
1,210 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 250 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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