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

在处理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();
        }
    }
}

原文地址:https://www.cnblogs.com/Jesuslovesme/p/8451907.html

时间: 2024-07-28 14:26:24

去除字符串内的一些额外字符方法的相关文章

JS中比較2个字符串内元素的不同(字符1, 字符2, 分隔符可选)

比較2个字符串内元素的不同(字符1, 字符2, 分隔符可选) 文件: diff.js // 演示样例使用方法 /* var str1 = "tie, mao, 55"; var str2 = "tie, mao, csdn"; var result = diff(str1, str2, ','); // 对象 var rs = "" + result; // " 55, csdn" var df1 = result.diff1

去除字符串中连续重复的字符

目的:把字符串中连续重复的字符赐除掉. 输入:序列:kkkhan888shioobo66 正确的返回结果应该是:hanshibo 思路解析 1 使用JAVA正则表达式,匹配出连续相同的字符或数字. 2 查找出匹配出来的序列,并取出来放到list里面 3 对list进行排序.把重复的序列排在前面.(该步可省略) 4找出连续重复的子序列,并把这些连续重复的子序列用空(字字符串)替换. 5 返回输出. code public class Test { public static void main(S

去除字符串两端的空格和字符

1 # -*- coding: utf-8 -*- 2 """ 3 去除字符串两端的空格和字符 4 lstrip().rstrip().strip() 5 返回字符串的拷贝 6 """ 7 x = ' aaa ' 8 9 print '|',x.lstrip(),'|',x.rstrip(),'|',x.strip(),'|' 10 # Output: | aaa | aaa | aaa | 11 12 x = 'xyxxyy hejyx yyx

JS中比较2个字符串内元素的不同(字符1, 字符2, 分隔符可选)

比较2个字符串内元素的不同(字符1, 字符2, 分隔符可选) 文件: diff.js // 示例用法 /* var str1 = "tie, mao, 55"; var str2 = "tie, mao, csdn"; var result = diff(str1, str2, ','); // 对象 var rs = "" + result; // " 55, csdn" var df1 = result.diff1; //

【iOS】去除字符串首尾空格或某字符

在iOS的实际开发中,常会出现需要去除空格的情况,总结有三种情况: 去除字符串首尾连续字符(如空格): 去除字符串首部连续字符(如空格): 去除字符串尾部连续字符(如空格): 去除字符串首尾连续字符(如空格) NSString *a = @" a sdf "; [a stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 去除字符串首部连续字符(如空格): NSString

asp.net 关于字符串内范围截取的一点方法总结

前两天有一位网友提出了一个字符串内截取字符串的问题,除了用普通的字符串截取的方式外,我推荐的是用LINQ方式来截取.两者实际上差别不是很大,都是采用字符串截取方式,但后者从写法和观察效果会比前者简单实用得多. 废话不多说,直接进入正题: 问题是:是这样的字符串,"dfsdg<2434>,dgdfg<35346>,dtr35<3w543>",提取"<"和">"里面的内容? 对于这样问题首先我们思路应该

JS去除字符串前后空格的几种方法

test // 1,使用原生的字符串trim方法 var str = " ss "; str.trim(); // 2 使用正则表达式将空白字符串替换成空字符串 str.replace (/^\s*|\s*$/g,"); // 如果浏览器不支持trim方法,可在代码前加上 if(!String.prototype.trim) { String.prototype.trim = function () { reuren this.replace(/^\s*|\s*$/g,&qu

JS 去除字符串中的最后一个字符

var str = 'Hello World!'; str = str.substr(0,str.length-1); alert(str);

递归实现一个去除字符串中“重复出现的字符”

The string you will received as a parameter has too many characters. Your job is to remove characters from the string in the following order:    1.    Find the smallest i such that the i-th character and the (i+1)-th character of the string are same.