none
Remove spec char from a string using a index RRS feed

  • Question

  • Hi, 

    Can anyone help me with how to remove a spec char from a string using a index? I should be able to use the string.Remove method but I don't know how. I want it to be something like:

    public static string remove_char(string str, int n) { return str.Remove(n, 1); }


    Please help! =)


    For example:

    Input: "Newyork"

    Output:

    Neyork

    Newyrk

    Neyork

    Friday, December 28, 2018 9:13 PM

All replies

  • The String.Remove() is in the links, and Bing and Google are your friends.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/trimming

    https://www.dotnetperls.com/remove

    https://www.c-sharpcorner.com/UploadFile/mahesh/add-remove-replace-strings-in-C-Sharp/


    Friday, December 28, 2018 9:45 PM

  • Can anyone help me with how to remove a spec char from a string using a index? I should be able to use the string.Remove method but I don't know how. 

    string str = "NewYork";
    Console.WriteLine(str);
    str = str.Remove(str.IndexOf('w'), 1);
    Console.WriteLine(str);
    
    

    Output:

    NewYork
    NeYork

    - Wayne

    Saturday, December 29, 2018 12:13 AM

  • string str = "NewYork";
    Console.WriteLine(str);
    str = str.Remove(str.IndexOf('w'), 1);
    Console.WriteLine(str);
    


    For a more robust approach that won't throw an exception if the character
    isn't in the string:

    string str = "NewYork";
    Console.WriteLine(str);
    if(str.Contains('w')) str = str.Remove(str.IndexOf('w'), 1);
    Console.WriteLine(str);
    
    // or:
    
    string str = "NewYork";
    Console.WriteLine(str);
    int ret = str.IndexOf('w');
    if(ret != -1) str = str.Remove(ret , 1);
    Console.WriteLine(str);
    
    

    - Wayne

    Saturday, December 29, 2018 1:25 AM
  • Hi Tasha.K,

    Thank you for posting here.

    For your question, you want to know how to remove a spec char from a string by using the index.

    Here is a simple code.

    class Program
        {
            static void Main(string[] args)
            {
                string str = "Newyork";
                Console.WriteLine("intput:  "+str);
                char m1 = 'w';
                char m2 = 'o';
                if(str.Contains(m1)&&str.Contains(m2))
                {
                    string str1 = remove_char(str, m1);
                    Console.WriteLine("output:  " + str1);
                    string str2 = remove_char(str, m2);
                    Console.WriteLine("output:  " + str2);
                }
                Console.ReadKey();
            }
            public static string remove_char(string str, char m)
            {
                int n = str.IndexOf(m);
                return str.Remove(n, 1);
            }
    }
    

    Result:

    Best regards,

    Jack J Jun.


    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.

    Monday, December 31, 2018 3:08 AM
    Moderator