locked
Trim c# RRS feed

  • Question

  • Hi

    How can i solve this problem ? I want to get this output, what would be the code? Console.Write("Enter your first name: "); string firstName = Console.ReadLine(); Console.WriteLine($"Your firstName is: {firstName.Trim()}"); If someone types two names with spaces, how do I get output without spaces. Use Trim but it only works with one word, it doesn't work with two words with spaces

    Tuesday, June 16, 2020 11:33 PM

Answers

  • Try this too:

    Console.WriteLine( $"Your firstName is: {firstName.Replace(" ", "")}" );


    Wednesday, June 17, 2020 1:52 AM

All replies

  • Use this to trim trailing spaces.

    var userinput = Console.ReadLine();
    
    if (!string.IsNullOrWhiteSpace(userinput))
    {
        userinput = userinput.TrimEnd();
        Console.WriteLine($"[{userinput}]");
        Console.ReadLine();
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 17, 2020 12:42 AM
  • Try this too:

    Console.WriteLine( $"Your firstName is: {firstName.Replace(" ", "")}" );


    Wednesday, June 17, 2020 1:52 AM