c# - C# 中的修剪函数

标签 c# string c#-2.0 trim

<分区>

Possible Duplicate:
Removing all spaces in a string

你好,

    // Input string
    string st = "  This is an example string. ";

    // Call Trim instance method.
    // This returns a new string copy.
    st = st.Trim();

    // output 
    "This is an example string."

我怎样才能输出

"Thisisanexamplestring."

我想删除整个句子之间的所有空格。谢谢。

最佳答案

Trim 只会删除字符串开头和结尾的字符。您想要替换所有出现的空格。使用 String.Replace为此:

st = st.Replace(" ", string.Empty); // Thisisanexamplestring.

请注意,不需要 String.Trim,因为我们替换了所有空间实例。

关于c# - C# 中的修剪函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5777514/

相关文章:

c# - select/epoll/kqueue 的 C# 等效函数是什么?

c# - 如何从远程客户端机器传输到开发团队软件崩溃?

javascript - 重复字符串 - Javascript

sql-server - EF : The text data type cannot be selected as DISTINCT because it is not comparable

c# - 在解析完成之前遇到流结束?

c# - 如何在 dot net core 3.0/3.1 的 razor 页面中添加区域?

c# - 在 .NET 中将 Int 数组写入 Stream

C+ 的 Java 等效项 +'s “std::string::find_first_not_of”

c#-2.0 - 如何获取将在嵌套数据列表中选择的单选按钮列表的值,以便稍后使用 c# asp.net 2 在多插入中使用它

multithreading - .Net 线程中的关键区域无法按预期工作