locked
Removing spaces from a string RRS feed

  • Question

  •  

    HI Ji Cheng Wang,

     I have a string "This is a log file for 10th january 2008",i need to remove all the spaces that are present in the string but without using buit in functions   provided by C#.

    Please let me know how can i do that.

     

    with regards,

    litu

    Monday, January 21, 2008 4:58 AM

Answers

  • Hi litu,

     

    Compared to C language, there is no a terminal char called '\0' in a C# string. If you would like to remove the spaces without using C# built in functions, you can try to change the sample codes as follows:

    Code Snippet
                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();
                foreach (char ch in strSpace)
                {
                    if (ch != ' ')
                    {
                        sb.Append(ch);
                    }
                }

    Or

    Code Snippet

                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();

                int len = strSpace.Length;
                for (int i = 0; i < len; i++)
                {
                    if (strSpace[i] != ' ')
                    {
                        sb.Append(strSpace[i]);
                    }
                }

     

    Hope this helps,

    Regards,

    Thursday, January 24, 2008 7:16 AM

All replies

  • Sounds like some kind of school assignment if you're not allowed to use "built in functions". I don't think anyone here will take care of your homework.

     

     

     

    Monday, January 21, 2008 7:09 AM
  • What constitutes a "built in fuction"?

    I assume that String.Replace() is out, but what about Regex.Replace() or String.ToCharArray()?

    Monday, January 21, 2008 5:56 PM
  • Hi litu,

     

    You can try the following sample codes to remove the white spaces from a string and see if it is what you need.

    Code Snippet

                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();
                foreach (Char ch in strSpace)
                {
                    if (!Char.IsWhiteSpace(ch))
                    {
                        sb.Append(ch);
                    }
                }

     

    Hope this helps,

    Wednesday, January 23, 2008 5:25 AM
  •  Ji Cheng Wang - MSFT wrote:

    Hi litu,

     

    You can try the following sample codes to remove the white spaces from a string and see if it is what you need.

    Code Snippet

                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();
                foreach (Char ch in strSpace)
                {
                    if (!Char.IsWhiteSpace(ch))
                    {
                        sb.Append(ch);
                    }
                }

     

    Hope this helps,

     

    Hi,

    Please tell me what does a string ends with in C#.

    Thanks for the code but please let me know , i want to remove spaces without using built in functions in C#

    Thursday, January 24, 2008 6:56 AM
  • Hi litu,

     

    Compared to C language, there is no a terminal char called '\0' in a C# string. If you would like to remove the spaces without using C# built in functions, you can try to change the sample codes as follows:

    Code Snippet
                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();
                foreach (char ch in strSpace)
                {
                    if (ch != ' ')
                    {
                        sb.Append(ch);
                    }
                }

    Or

    Code Snippet

                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();

                int len = strSpace.Length;
                for (int i = 0; i < len; i++)
                {
                    if (strSpace[i] != ' ')
                    {
                        sb.Append(strSpace[i]);
                    }
                }

     

    Hope this helps,

    Regards,

    Thursday, January 24, 2008 7:16 AM
  •  Ji Cheng Wang - MSFT wrote:

    Hi litu,

     

    Compared to C language, there is no a terminal char called '\0' in a C# string. If you would like to remove the spaces without using C# built in functions, you can try to change the sample codes as follows:

    Code Snippet
                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();
                foreach (char ch in strSpace)
                {
                    if (ch != ' ')
                    {
                        sb.Append(ch);
                    }
                }

    Or

    Code Snippet

                string strSpace = "This is a log file for 10th january 2008";
                StringBuilder sb = new StringBuilder();

                int len = strSpace.Length;
                for (int i = 0; i < len; i++)
                {
                    if (strSpace[i] != ' ')
                    {
                        sb.Append(strSpace[i]);
                    }
                }

     

    Hope this helps,

    Regards,

     

    Hi Ji Cheng Wang ,

    Thanks a lot for your help.

     

    with regards,

    litu

     

     

    Friday, January 25, 2008 5:13 AM