删除字符串文字的格式

发布于 2024-12-01 19:12:44 字数 286 浏览 1 评论 0原文

给定 c# 代码:

string foo = @"
           abcde
           fghijk";

我试图删除所有格式,包括行之间的空格。

到目前为止,代码

foo = foo.Replace("\n","").Replace("\r", "");

可以工作,但第 2 行和第 3 行之间的空白仍然保留。

我认为正则表达式是唯一的解决方案?

谢谢。

Given the c# code:

string foo = @"
           abcde
           fghijk";

I am trying to remove all formatting, including whitespaces between the lines.

So far the code

foo = foo.Replace("\n","").Replace("\r", "");

works but the whitespace between lines 2 and 3 and still kept.

I assume a regular expression is the only solution?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

微暖i 2024-12-08 19:12:44

我假设你想保留多行,如果不想,我会选择 CAbbott答案

var fooNoWhiteSpace = string.Join(
    Environment.NewLine,
    foo.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
       .Select(fooline => fooline.Trim())
);
  1. 它的作用是将字符串分割成行(foo.Split),
  2. 修剪每行开头和结尾的空格 (.Select(fooline => fooline.Trim())),
  3. 然后将它们重新组合在一起,并在中间添加一个新行(string.Join)。

I'm assuming you want to keep multiple lines, if not, i'd choose CAbbott's answer.

var fooNoWhiteSpace = string.Join(
    Environment.NewLine,
    foo.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
       .Select(fooline => fooline.Trim())
);
  1. What this does it split the string into lines (foo.Split),
  2. trim whitespace from the start and end of each line (.Select(fooline => fooline.Trim())),
  3. then combine them back together with a new line inbetween (string.Join).
无所的.畏惧 2024-12-08 19:12:44

您可以使用正则表达式:

foo = Regex.Replace(foo, @"\s+", "");

You could use a regular expression:

foo = Regex.Replace(foo, @"\s+", "");
仅此而已 2024-12-08 19:12:44

这个怎么样?

string input = @" 
           abcde 
           fghijk"; 
string output = "";
string[] parts = input.Split('\n');

foreach (var part in parts)
{
    // If you want everything on one line... else just + "\n" to it
    output += part.Trim();
}

这应该删除所有内容。

How about this?

string input = @" 
           abcde 
           fghijk"; 
string output = "";
string[] parts = input.Split('\n');

foreach (var part in parts)
{
    // If you want everything on one line... else just + "\n" to it
    output += part.Trim();
}

This should remove everthing.

嘴硬脾气大 2024-12-08 19:12:44

如果空格都是空格,您可以使用

foo.Replace(" ", "");

对于其中可能存在的任何其他空格,执行相同的操作。例子:

foo.Replace("\t", "");

If the whitespace is all spaces, you could use

foo.Replace(" ", "");

For any other whitespace that may be in there, do the same. Example:

foo.Replace("\t", "");
掐死时间 2024-12-08 19:12:44

只需添加一个 Replace(" ", "") 来处理字符串文字,这意味着所有空格都是字符串的一部分。

Just add a Replace(" ", "") your dealing with a string literal which mean all the white space is part of the string.

我还不会笑 2024-12-08 19:12:44

尝试这样的操作:

string test = @"
            abcde
            fghijk";    

编辑:添加代码以仅过滤掉空格。

string newString = new string(test.Where(c => Char.IsWhiteSpace(c) == false).ToArray());

产生以下内容:abcdefghijk

Try something like this:

string test = @"
            abcde
            fghijk";    

EDIT: Addded code to only filter out white spaces.

string newString = new string(test.Where(c => Char.IsWhiteSpace(c) == false).ToArray());

Produces the following: abcdefghijk

满天都是小星星 2024-12-08 19:12:44

我写了一些类似于 George Duckett 的东西,但将我的逻辑放入字符串扩展方法中,以便其他人更容易阅读/使用:

public static class Extensions
{
    public static string RemoveTabbing(this string fmt)
    {
        return string.Join(
            System.Environment.NewLine,
            fmt.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                .Select(fooline => fooline.Trim()));
    }
}

你可以这样称呼它:

string foo = @"
       abcde
       fghijk".RemoveTabbing();

我希望对某人有帮助

I've written something similar to George Duckett but put my logic into a string extension method so it easier for other to read/consume:

public static class Extensions
{
    public static string RemoveTabbing(this string fmt)
    {
        return string.Join(
            System.Environment.NewLine,
            fmt.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                .Select(fooline => fooline.Trim()));
    }
}

you can the call it like this:

string foo = @"
       abcde
       fghijk".RemoveTabbing();

I hope that helps someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文