Fastest way to remove white spaces in string

后端 未结 13 1199
粉色の甜心
粉色の甜心 2020-11-27 18:39

I\'m trying to fetch multiple email addresses seperated by \",\" within string from database table, but it\'s also returning me whitespaces, and I want to remove the whitesp

13条回答
  •  自闭症患者
    2020-11-27 18:56

    You should try String.Trim(). It will trim all spaces from start to end of a string

    Or you can try this method from linked topic: [link]

        public static unsafe string StripTabsAndNewlines(string s)
        {
            int len = s.Length;
            char* newChars = stackalloc char[len];
            char* currentChar = newChars;
    
            for (int i = 0; i < len; ++i)
            {
                char c = s[i];
                switch (c)
                {
                    case '\r':
                    case '\n':
                    case '\t':
                        continue;
                    default:
                        *currentChar++ = c;
                        break;
                }
            }
            return new string(newChars, 0, (int)(currentChar - newChars));
        }
    

提交回复
热议问题