c#中的string.Trim()函数 [英] string.Trim() function in c#

查看:135
本文介绍了c#中的string.Trim()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个修剪功能不起作用..



why this trim function is not working..

string str = content.trim(new char[] {',', '.', ';'})





i想从我的字符串中删除所有逗号,句号和分号,但这个函数不起作用....



i want to remove all the commas, full stops and semi-colons from my string, but this function is not working....

推荐答案

修剪将从开始或结束中删除字符。不在字符之间由Trim函数删除。

您可以使用替换或删除函数或Reg Ex表达式。



Trim will remove character from start or end. Not in between characters are removed by Trim function.
You can use Replace or Remove functions or Reg Ex expression.

Regex pattern = new Regex("[;,.]");
string str = pattern.Replace(content, "");


修剪仅适用于字符串的左端和右端,它对中间没有任何作用!



试试正则表达式:

Trim only works on the left and right ends of the string, it doesn't do anything about the middle!

Try a regex:
string without = Regex.Replace(inputString, @"[,\.;]+", "");


你应该使用Replace()方法而不是Trim()



修剪消除了前导和尾随空格。我们需要从字符串的开头或结尾删除空格。我们使用.NET Framework的Trim方法来有效地执行此操作。此方法删除指定的任何字符。



You are supposed to use Replace() method instead of Trim()

Trim eliminates leading and trailing whitespace. We need to remove whitespace from the beginning or ending of a string. We use the .NET Framework's Trim method to do this efficiently. This method removes any characters specified.

string input = "Hi,I'm a sentence having special characters.;";

input = input.Replace("'", "").Replace(",", "").Replace(";", "").Replace(".", "");


这篇关于c#中的string.Trim()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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