Solved

Split the string based on Decimal point

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now