C# 删除字符串中除字母和数字之外的其它字符,C#将字符串中的大写转小写,小写转大写

时间:2022-03-23 09:10:47
        private static string RemoveNotLetter(string title)
        {
            var listSign = new List<string> { "|", "'", ",", "&", ".", "!" };
            var notLetter = Regex.Split(title, @"[a-zA-Z]/[0-9]", RegexOptions.IgnoreCase).Where(r => r.Trim() != string.Empty).ToList();
            var newLetter = new List<string>();

            for (int i = notLetter.Count - 1; i >= 0; i--)
            {
                if (notLetter[i].Trim().Length == 0)
                {
                    notLetter.RemoveAt(i);
                    continue;
                }

                if (notLetter[i].Trim().Length > 1)
                {
                    for (int j = 0; j < notLetter[i].Trim().Length; j++)
                    {
                        newLetter.Add(notLetter[i].Trim().Substring(j, 1));
                    }
                    notLetter.RemoveAt(i);
                }
            }

            notLetter.AddRange(newLetter);
            foreach (string sign in notLetter)
            {
                if (sign.Trim().Length == 0) continue;

                if (!listSign.Contains(sign.Trim()))
                {
                    title = title.Replace(sign.Trim(), "");
                }
            }
            return title;
        }

        private string  str(string strs){            
            string str=strs;            
            char[] ch = str.ToCharArray();            
            System.Text.RegularExpressions.Regex R = new System.Text.RegularExpressions.Regex("[A-Z]");            
            str = "";           
            foreach (char s in ch)            
            {                
                if (R.IsMatch(s.ToString()))                
                {                    
                    str += s.ToString().ToLower();                
                }                
                else                
                {                    
                    str += s.ToString().ToUpper();                
                }           
            }  
            return str;
        }