none
string trimming using white space c#. help!

    Question

  • Hi friends, I am a c# learner using vs community 2017, i don't know much more about c#. I have a string.

    suppose,

    string abc = "Employee ID: 1000    Emp Name- someName";

    There are total two white spaces till we reach '1000' (First one is in between 'Employee' and 'ID'. Second one is in between ':' and '1000'). After '1000' there are 4 white spaces until we reach 'Emp'. The string parts '1000', 'someName' are dynamic. Any size of length values may come over there.

    My problem is, i need to trim out a part from that given string in between some specific white spaces.

    In another words, I need a part from the above string in between after the 2nd white space and before the 3rd white space.

    I don't know is it possible or not. Please help to solve this. Thanks in advance.


    Sunday, April 16, 2017 10:38 PM

Answers

  • Karen, that won't work if someName has numeric digits in it. I suggest Split might be more foolproof.

    string abc = "Employee ID: 1000    Emp Name- someName";
    string[] parts = abc.Split(" ".ToCharArray());
    
    string result = parts[2];

    • Marked as answer by Dileep CV Monday, April 17, 2017 6:17 AM
    Monday, April 17, 2017 12:59 AM
  • Hi Dileep CV,

    Thank you for posting here.

    For your question, please try the following code.

     static void Main(string[] args)
            {
                string abc = "Employee ID: 1000    Emp Name- someName";
                var value1 = abc.Split(' ')[2];
                Console.WriteLine("value1={0}", value1);
    
                int num1 = abc.IndexOf(" ");
                int num2 = abc.IndexOf(" ", num1 + 1);
                int num3 = abc.IndexOf(" ", num2 + 1);
                string value2 = abc.Substring(num2 + 1, num3 - num2);
                Console.WriteLine("value2={0}", value2);
    
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dileep CV Monday, April 17, 2017 6:14 AM
    Monday, April 17, 2017 2:47 AM
    Moderator

All replies

  • The following removes all characters and leave the numeric value of 1000.

    string abc = "Employee ID: 1000    Emp Name- someName";
    var result = Regex.Replace(abc, "[^0-9]", "").Trim();


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 16, 2017 11:58 PM
    Moderator
  • Karen, that won't work if someName has numeric digits in it. I suggest Split might be more foolproof.

    string abc = "Employee ID: 1000    Emp Name- someName";
    string[] parts = abc.Split(" ".ToCharArray());
    
    string result = parts[2];

    • Marked as answer by Dileep CV Monday, April 17, 2017 6:17 AM
    Monday, April 17, 2017 12:59 AM
  • Karen, that won't work if someName has numeric digits in it. I suggest Split might be more foolproof.

    string abc = "Employee ID: 1000    Emp Name- someName";
    string[] parts = abc.Split(" ".ToCharArray());
    
    string result = parts[2];

    But of course that could be the case, usually names don't have numbers in them at least that is how I see it.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, April 17, 2017 1:23 AM
    Moderator
  • Hi Dileep CV,

    Thank you for posting here.

    For your question, please try the following code.

     static void Main(string[] args)
            {
                string abc = "Employee ID: 1000    Emp Name- someName";
                var value1 = abc.Split(' ')[2];
                Console.WriteLine("value1={0}", value1);
    
                int num1 = abc.IndexOf(" ");
                int num2 = abc.IndexOf(" ", num1 + 1);
                int num3 = abc.IndexOf(" ", num2 + 1);
                string value2 = abc.Substring(num2 + 1, num3 - num2);
                Console.WriteLine("value2={0}", value2);
    
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dileep CV Monday, April 17, 2017 6:14 AM
    Monday, April 17, 2017 2:47 AM
    Moderator
  • Hi Dileep CV,

    You could try the following code using regex to extract the part "Emp Name" and split on "-". Here you can get both Name and Name with number. You can change the Regex as per your requirement. I am guessing you want Name like the format we use in username. Hope this helps you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                string abc = "Employee ID: 1000    Emp Name- someName159";
                var result = Regex.Match(abc, @"Emp Name-.\w*.\w+");
                string parts= result.ToString();
                Console.WriteLine(parts.Split('-')[1].ToString());
            }
        }
    }

    Output:

    someName159


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Monday, April 17, 2017 5:36 AM
    Moderator
  • Thanks for replying...

    Suppose the 'someName' part has some numeric value then?

    that's why i didn't ask "i need to trim out the numeric value".


    • Edited by Dileep CV Monday, April 17, 2017 6:06 AM
    Monday, April 17, 2017 5:59 AM
  • Thank you friend. It worked! :) You saved my day. :)
    Monday, April 17, 2017 6:15 AM
  • Thanks friend. It is also worked for me. Simply you solved this with just only 3 lines of codes. Great. :)
    Monday, April 17, 2017 6:19 AM