• 去除字符串内的一些额外字符方法


    在处理String对象时,有时候需要去除一些额外的字符,如:空格等

    主要的方法:

      string.Trim()  常用   无参默认去除字符串两端的空格,多个空格也能一次性去掉;但是注意只是两段,内部无法控制!!

      string.Trim(char [])    当有多个特定字符需要去除的时候,用这种方法

      string.TrimStart( char c) 不常用  针对字符串首端去除指定标记

      string.TrimStart( char [])  针对字符串首端去除指定标记组

      string.TrimEnd( char c) 不常用 针对字符串末端去除指定标记

      string.TrimEnd( char [])  针对字符串末端去除指定标记组

    string.Trim()默认方法是去除字符串两头的空格,中间的空格它去除不了,并且如果某一端空格两个及以上,它只能消除一个空格!!!

    string.TrimStart只能对字符串开始端处理;string.TrimEnd只能对字符串末端处理!!!

    下面是以上6种情况的学习操作的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                string words = "  _hh  _wo si _ ";
                Console.WriteLine("初始words:" + words);
                Console.WriteLine("初始words的字符个数:" + words.Length);
                Console.WriteLine();
                Console.WriteLine("========================");
                Console.WriteLine();
    
                string newWords = words.Trim();
                Console.WriteLine("使用Trim()方法所执行的结果:" + newWords);
                Console.WriteLine("使用Trim()方法后字符的个数:" + newWords.Length);
    
                char[] com = new char[] { ' ','_'};
                string newnewWords = words.Trim(com);
                Console.WriteLine("使用Trim(Char [])方法所执行的结果:" + newnewWords);
                Console.WriteLine("使用Trim(Char [])方法后字符的个数:" + newnewWords.Length);
    
                Console.WriteLine();
                Console.WriteLine("========================");
                Console.WriteLine();
    
               string triS = words.TrimStart(' ');
               Console.WriteLine("使用TrimStart(char c )方法所执行的结果:" + triS);
               Console.WriteLine("使用TrimStart(char c)方法所执行的结果:" + triS.Length);
    
               char[] com1 = new char[] { ' ', '_' };
               triS = words.TrimStart(com1);
               Console.WriteLine("使用TrimStart(char [])方法所执行的结果:" + triS);
               Console.WriteLine("使用TrimStart(char [])方法所执行的结果:" + triS.Length);
    
               Console.WriteLine();
               Console.WriteLine("========================");
               Console.WriteLine();
    
               string triE = words.TrimEnd(' ');
               Console.WriteLine("使用TrimEnd(char c )方法所执行的结果:" + triE);
               Console.WriteLine("使用TrimEnd(char c)方法所执行的结果:" + triE.Length);
    
               char[] com2 = new char[] { ' ', '_' };
               triE = words.TrimEnd(com2);
               Console.WriteLine("使用TrimEnd(char [])方法所执行的结果:" + triE);
               Console.WriteLine("使用TrimEnd(char [])方法所执行的结果:" + triE.Length);
    
                Console.ReadKey();
            }
        }
    }

     

    问题来了,如果我想得到一个连续的,连中间的空格也剔除掉的字符串,该怎么操作呢~?

    思想:用Split方法借助要去除的标记当作参数把字符串分割成了一个字符数组,然后把这个字符数组里面的每个字符连续输出来

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                string words = ". I_ am   shang, ,qing_feng .";  
    
               // ArrayList list = new ArrayList();
                string[] word = words.Split('.', '_',' ', ',');//空格不写的情况下, I am   shang qingfeng 这不是我们想要的结果,所以把空格符也当作分割符
    
                foreach (string s in word)
                    Console.Write(s);//Iamshangqingfeng
                Console.ReadKey();
            }
        }
    }

     

  • 相关阅读:
    第十三周助教总结
    C语言I博客作业09
    第十二周助教总结
    C语言I博客作业08
    第十一周助教总结
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I作业07
    C语言I作业06
  • 原文地址:https://www.cnblogs.com/Jesuslovesme/p/8451907.html
Copyright © 2020-2023  润新知