使用 String 构建器或 string 类构建带有空格的字符串

发布于 2024-11-17 00:02:24 字数 460 浏览 2 评论 0原文

我需要创建一个长字符串,大约 360 个字符,具有不同的值,并将其发送到服务。我知道每个部分的位置,并且我需要能够插入值和空格,直到到达下一个位置。 以下只是我开始的一个例子;也就是说,我需要从位置 0 开始,下一个值需要从位置 5(abc) 开始。到目前为止,我已经能够像这样连接:“1234abc”,但我需要的是“1234[space]abc[space][space]”谢谢你的帮助。

    //sbTrialSpaces
    private void TrialSpaces()
    {

        string str1 = "1234";
        string str2 = "abc";
        string finalStr;//Has to be 10 positions
        //like this "1234 abc  "

    }

I need to make a long string, about 360 characters, with different values that I am sending to a service. I know the positions for each section and I need to be able to insert values and blanks until I get to the next position.
The following is just an example that I started; per say I need to start in position 0 and the next value needs to go in position 5(abc). So far I've been able to concatenate like this: "1234abc", but what I need is "1234[space]abc[space][space]" Thanks for your help.

    //sbTrialSpaces
    private void TrialSpaces()
    {

        string str1 = "1234";
        string str2 = "abc";
        string finalStr;//Has to be 10 positions
        //like this "1234 abc  "

    }

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

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

发布评论

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

评论(7

情绪少女 2024-11-24 00:02:24
string.Format("{0,-5}{1,-5}{2,-5}", val1, val2, val3);

这将为每个值留出五个空格,即使它们的宽度小于五个字符。 “-”表示这些值将左对齐。

string.Format("{0,-5}{1,-5}{2,-5}", val1, val2, val3);

This will make five spaces for each of the values, even if they are less than five characters wide. The "-" means that the values will be left-justified.

陌上青苔 2024-11-24 00:02:24

只需按照您的建议使用 StringBuilder 即可。

StringBuilder sb = new StringBuilder();
sb.Append(str1);
sb.Append(" ");
sb.Append(str2);
sb.Append("  ");
string finalString = sb.ToString();

Just use the StringBuilder like you suggested.

StringBuilder sb = new StringBuilder();
sb.Append(str1);
sb.Append(" ");
sb.Append(str2);
sb.Append("  ");
string finalString = sb.ToString();
遗弃M 2024-11-24 00:02:24
var finalStr = ("1234" + " " + "abc").PadRight(10, ' ');
var finalStr = ("1234" + " " + "abc").PadRight(10, ' ');
零度° 2024-11-24 00:02:24

您可以使用 String.Join

String.Join(" ", myStrings);

假设 myStrings 是字符串的 IEnumerable

You can use String.Join

String.Join(" ", myStrings);

Assuming myStrings is an IEnumerable of strings

while (finalStr.Length < 10)
    finalStr += " ";
while (finalStr.Length < 10)
    finalStr += " ";
彼岸花似海 2024-11-24 00:02:24

我推荐 固定长度 格式 FileHelpers 支持。

[FixedLengthRecord()] 
public class Order 
{ 
    [FieldFixedLength(5)] 
    public int OrderId; 

    [FieldFixedLength(30)] 
    [FieldTrim(TrimMode.Right)] 
    public string CustomerName; 

    [FieldFixedLength(10)] 
    public string SKU; 

    [FieldFixedLength(8)] 
    [FieldConverter(typeof(TwoDecimalConverter))] 
    public decimal Price; 

    [FieldFixedLength(8)] 
    [FieldConverter(ConverterKind.Date, "ddMMyyyy")] 
    public DateTime AddedDate; 
}

I recommend the Fixed Length format that FileHelpers supports.

[FixedLengthRecord()] 
public class Order 
{ 
    [FieldFixedLength(5)] 
    public int OrderId; 

    [FieldFixedLength(30)] 
    [FieldTrim(TrimMode.Right)] 
    public string CustomerName; 

    [FieldFixedLength(10)] 
    public string SKU; 

    [FieldFixedLength(8)] 
    [FieldConverter(typeof(TwoDecimalConverter))] 
    public decimal Price; 

    [FieldFixedLength(8)] 
    [FieldConverter(ConverterKind.Date, "ddMMyyyy")] 
    public DateTime AddedDate; 
}
黯然 2024-11-24 00:02:24
string finalStr = str1.PadRight(4,' ') + str2.PadRight(6,' ');

不需要字符串生成器,因为您是一次性添加所有内容。

string finalStr = str1.PadRight(4,' ') + str2.PadRight(6,' ');

There is no need for a stringbuilder because you are adding it all at once.

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