简体   繁体   中英

C# Append x occurrences of a character to a string

What is the best/recommended way to add x number of occurrences of a character to a string eg

String header = "HEADER";

The header variable needs to have, let's say 100 0 's, added to the end of it. But this number will change depending on other factors.

How about:

header += new string('0', 100);

Of course; if you have multiple manipulations to make, consider StringBuilder :

StringBuilder sb = new StringBuilder("HEADER");
sb.Append('0', 100); // (actually a "fluent" API if you /really/ want...)
// other manipluations/concatenations (Append) here
string header = sb.ToString();

这将在字符串中追加100个零字符:

header += new string('0', 100);

How about

string header = "Header";
header = header.PadRight(header.Length + 100, '0');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM