为什么我使用 StringBuilder 和 .replace 会得到意想不到的结果?
Why am I getting unexpected results with StringBuilder and .replace?
我正在尝试在字符串 (wildCard
) 中查找特定条件 (dogsWildcard
、catsWildcard
、birdsWildcard
),然后修改该字符串,使其具有与条件关联的数字。
比如我要冷到下面return:"*DOGS 111 *CATS 222 *BIRDS 333"
但是,它 return 看起来像:"*CATS *DOGS 222 *BIRDS*DOGS 111 *EVENTS *BIRDS*BIRDS *DOGS *CATS 333"
public string ProcessMerchantRefNbr()
{
var wildCard = "*DOGS *CATS *BIRDS";
string catsWildcard = "*CATS";
string dogsWildcard = "*DOGS";
string birdsWildcard = "*BIRDS";
var dogId = "111";
var catId = "222";
var birdId = "333";
var strMerchantRefNbr = new StringBuilder();
if (wildCard.Contains(catsWildcard)) strMerchantRefNbr.Append($"{catsWildcard} {wildCard.Replace(catsWildcard, catId)}");
if (wildCard.Contains(dogsWildcard)) strMerchantRefNbr.Append($"{dogsWildcard} {wildCard.Replace(dogsWildcard, dogId)}");
if (wildCard.Contains(birdsWildcard)) strMerchantRefNbr.Append($"{birdsWildcard} {wildCard.Replace(birdsWildcard, birdId)}");
return strMerchantRefNbr.ToString()
}
我做错了什么?
只是你不需要 Replace
.
var strMerchantRefNbr = new StringBuilder();
if (wildCard.Contains(catsWildcard)) strMerchantRefNbr.Append($"{catsWildcard} {catId} ");
if (wildCard.Contains(dogsWildcard)) strMerchantRefNbr.Append($"{dogsWildcard} {dogId} ");
if (wildCard.Contains(birdsWildcard)) strMerchantRefNbr.Append($"{birdsWildcard} {birdId} ");
// strMerchantRefNbr.ToString(): *CATS 222 *DOGS 111 *BIRDS 333
试试这个
public string ProcessMerchantRefNbr()
{
var wildCard = "*DOGS *CATS *BIRDS";
string[,] replacements = new string[,] { { "*DOGS", "*CATS", "*BIRDS" },
{ "111", "222", "333" } };
var sb = new StringBuilder(wildCard);
for (var i = 0; i <= replacements.GetLength(0); i++)
{
var wc = replacements[0, i];
var num = replacements[1, i];
sb.Replace(wc, wc + " " + num);
}
return sb.ToString();
}
结果
*DOGS 111 *CATS 222 *BIRDS 333
或者我会更灵活地创建这样的东西
string[,] replacements = new string[,] { { "*DOGS", "*CATS", "*BIRDS" },
{ "*DOGS 111 ...", "*CATS 222 ...", "*BIRDS 333 ..." } };
您正在为每个解析为 true
的条件附加一个替换字符串。实际上你可以省略检查 - 将原始字符串附加到构建器并在其上使用 Replace
:
var strMerchantRefNbr = new StringBuilder();
strMerchantRefNbr.Append(wildCard);
strMerchantRefNbr.Replace(catsWildcard, $"{catsWildcard} {catId}");
strMerchantRefNbr.Replace(dogsWildcard, $"{dogsWildcard} {dogId}");
strMerchantRefNbr.Replace(birdsWildcard, $"{birdsWildcard} {birdId}");
可以通过创建通配符 -> 替换对和使用循环来改进此代码。例如使用值元组:
var replacements = new List<(string Wildcard, string Replacement)>
{
("*CATS", "111"),
("*DOGS", "222"),
("*BIRDS", "333")
};
var strMerchantRefNbr = new StringBuilder();
strMerchantRefNbr.Append(wildCard);
foreach(var (wc, replacement) in replacements)
{
strMerchantRefNbr.Replace(wc, $"{wc} {replacement}");
}
我正在尝试在字符串 (wildCard
) 中查找特定条件 (dogsWildcard
、catsWildcard
、birdsWildcard
),然后修改该字符串,使其具有与条件关联的数字。
比如我要冷到下面return:"*DOGS 111 *CATS 222 *BIRDS 333"
但是,它 return 看起来像:"*CATS *DOGS 222 *BIRDS*DOGS 111 *EVENTS *BIRDS*BIRDS *DOGS *CATS 333"
public string ProcessMerchantRefNbr()
{
var wildCard = "*DOGS *CATS *BIRDS";
string catsWildcard = "*CATS";
string dogsWildcard = "*DOGS";
string birdsWildcard = "*BIRDS";
var dogId = "111";
var catId = "222";
var birdId = "333";
var strMerchantRefNbr = new StringBuilder();
if (wildCard.Contains(catsWildcard)) strMerchantRefNbr.Append($"{catsWildcard} {wildCard.Replace(catsWildcard, catId)}");
if (wildCard.Contains(dogsWildcard)) strMerchantRefNbr.Append($"{dogsWildcard} {wildCard.Replace(dogsWildcard, dogId)}");
if (wildCard.Contains(birdsWildcard)) strMerchantRefNbr.Append($"{birdsWildcard} {wildCard.Replace(birdsWildcard, birdId)}");
return strMerchantRefNbr.ToString()
}
我做错了什么?
只是你不需要 Replace
.
var strMerchantRefNbr = new StringBuilder();
if (wildCard.Contains(catsWildcard)) strMerchantRefNbr.Append($"{catsWildcard} {catId} ");
if (wildCard.Contains(dogsWildcard)) strMerchantRefNbr.Append($"{dogsWildcard} {dogId} ");
if (wildCard.Contains(birdsWildcard)) strMerchantRefNbr.Append($"{birdsWildcard} {birdId} ");
// strMerchantRefNbr.ToString(): *CATS 222 *DOGS 111 *BIRDS 333
试试这个
public string ProcessMerchantRefNbr()
{
var wildCard = "*DOGS *CATS *BIRDS";
string[,] replacements = new string[,] { { "*DOGS", "*CATS", "*BIRDS" },
{ "111", "222", "333" } };
var sb = new StringBuilder(wildCard);
for (var i = 0; i <= replacements.GetLength(0); i++)
{
var wc = replacements[0, i];
var num = replacements[1, i];
sb.Replace(wc, wc + " " + num);
}
return sb.ToString();
}
结果
*DOGS 111 *CATS 222 *BIRDS 333
或者我会更灵活地创建这样的东西
string[,] replacements = new string[,] { { "*DOGS", "*CATS", "*BIRDS" },
{ "*DOGS 111 ...", "*CATS 222 ...", "*BIRDS 333 ..." } };
您正在为每个解析为 true
的条件附加一个替换字符串。实际上你可以省略检查 - 将原始字符串附加到构建器并在其上使用 Replace
:
var strMerchantRefNbr = new StringBuilder();
strMerchantRefNbr.Append(wildCard);
strMerchantRefNbr.Replace(catsWildcard, $"{catsWildcard} {catId}");
strMerchantRefNbr.Replace(dogsWildcard, $"{dogsWildcard} {dogId}");
strMerchantRefNbr.Replace(birdsWildcard, $"{birdsWildcard} {birdId}");
可以通过创建通配符 -> 替换对和使用循环来改进此代码。例如使用值元组:
var replacements = new List<(string Wildcard, string Replacement)>
{
("*CATS", "111"),
("*DOGS", "222"),
("*BIRDS", "333")
};
var strMerchantRefNbr = new StringBuilder();
strMerchantRefNbr.Append(wildCard);
foreach(var (wc, replacement) in replacements)
{
strMerchantRefNbr.Replace(wc, $"{wc} {replacement}");
}