Solved

Split the string based on Decimal point

Posted on 2011-02-21
3
1,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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