清除字符串中的字符 [英] Cleaning characters from a string

查看:94
本文介绍了清除字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  schoollist = '  102''  11''  23''  34 ' 
string [] SchoolListArr = schoollist.Split(' );

for int i = 0 ; i < SchoolListArr.Length; i ++)
{
/ / Console.WriteLine(学校列表:+ SchoolListArr [i]);

// String s = null;

String [] schools = new String [ 100 ];

学校[i] = SchoolListArr [i];

Console.WriteLine( 学校名单: +学校[i ]);

}
Console.ReadLine();

// 现在我的问题是如何从字符串中删除('')所以我有

102
11
// 而不是
' 102'
' 11 '

// 谢谢

解决方案

首先,您在问题中输入的代码将无法编译。

string schoollist ='102','11','23','34'需要 string schoollist ='102','11','23',' 34';



为了替换字符串中的值,您可以非常简单地使用字符串。替换()已满足hod。

schoollist = schoollist.Replace(\',string.Empty);



有关string.Replace的更多信息: MSDN文档 [ ^ ]


尝试:

  string  schoollist =  '102','11','23','34'; 
schoollist = schoollist.Replace( ' ); // < ==添加此行
string [] SchoolListArr = schoollist.Split(' ,');



您还需要用引号括住 schoollist 字符串,并在行尾添加分号,否则它会赢得'编译。



希望这会有所帮助。


这是你可以使用string.Trim(params char)的一种方法。 [])



 Console.WriteLine(学校名单:+学校[i] .Trim(char.Parse(') ))); 


string schoollist ='102','11','23','34'
string[] SchoolListArr = schoollist.Split(',');

for (int i = 0; i < SchoolListArr.Length; i++)
{
    //Console.WriteLine("The school List:" + SchoolListArr[i]);

 //   String s = null;

    String[] schools = new String[100];

    schools[i] = SchoolListArr[i];

    Console.WriteLine("The school List:" + schools[i]);

}
Console.ReadLine();

// now my question is how do I strip ( ' ') from the  string so that I have 

102
11
// instead of 
'102'
'11'

// thank you

解决方案

For starters, the code you have put in your question will not compile.
string schoollist ='102','11','23','34' would need to be string schoollist ="'102','11','23','34'";

In order to replace values in a string, you could very simply use the string.Replace() method.
schoollist = schoollist.Replace("\'", string.Empty);

For more info on string.Replace: MSDN documentation[^]


Try:

string schoollist = "'102','11','23','34'";
schoollist = schoollist.Replace("'", ""); // <== add this line
string[] SchoolListArr = schoollist.Split(',');


You also need to surround your schoollist string with quotes and add a semicolon at the end of the line, otherwise it won't compile.

Hope this helps.


This is one way you could do it, using string.Trim(params char[])

Console.WriteLine("The school List:" + schools[i].Trim(char.Parse("'")));


这篇关于清除字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆