Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

c# string

I have a name John Smith
and I want to have format as jsmith

How can I do that in c#?

Thanks
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Try.. sorry could not test..now..

STRING str = "John Smith"
String newstr  = SUBSTRING (str,1) + SUBSTRING(str,INDEXOF(' '), LEN(str) - INDEXOF(' ') )
Avatar of ITsolutionWizard

ASKER

sorry not working
Let me check it again..
//Your code goes here
            String str = "John Smith";
            String newstr  =   str.Substring(0,1) + str.Substring(str.Length-str.IndexOf(' ')-1);
            Console.WriteLine (newstr);

Ouput

JSmith
Avatar of David Johnson, CD
using System;

namespace rr_28975164
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fullname = "John Smith";
            string result;
            string first;
            string last;
            string[] names = Fullname.Split(' ');
            first = names[0][0].ToString();
            last = names[1];
            string output = first + last;
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India 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