简体   繁体   English

用另一个字符串替换字符串中的字符

[英]Replacing characters in a string with another string

So what I am trying to do is as follows : 所以我想做的如下:

example of a string is A4PC 字符串的示例是A4PC

I am trying to replace for example any occurance of "A" with "[A4]" so I would get and similar any occurance of "4" with "[A4]" 例如,我试图用“ [A4]”替换任何出现的“ A”,所以我会用“ [A4]”替换并出现类似的“ 4”

"[A4][A4]PC" “ [A4] [A4] PC”

I tried doing a normal Replace on the string but found out I got 我尝试对字符串进行常规替换,但发现我得到了

"[A[A4]]PC" “ [A [A4]] PC”

string badWordAllVariants =
                restriction.Value.Replace("A", "[A4]").Replace("4", "[A4]")

since I have two A's in a row causing an issue. 因为我连续有两个A导致问题。

So I was thinking it would be better rather than the replace on the string I need to do it on a character per character basis and then build up a string again. 因此,我认为这要比替换字符串更好,因为我需要逐个字符地进行替换,然后重新构建一个字符串。

Is there anyway in Linq or so to do something like this ? 无论如何,Linq中是否有要做类似的事情?

You don't need any LINQ here - String.Replace works just fine: 您在这里不需要任何LINQ- String.Replace可以正常工作:

string input = "AAPC";
string result = input.Replace("A", "[A4]"); // "[A4][A4]PC"

UPDATE: For your updated requirements I suggest to use regular expression replace 更新:对于您的更新要求,我建议使用正则表达式替换

string input = "A4PC";
var result = Regex.Replace(input, "A|4", "[A4]"); // "[A4][A4]PC"

This works well for me: 这对我来说很好:

        string x = "AAPC";
        string replace = x.Replace("A", "[A4]");

EDIT: 编辑:

Based on the updated question, the issue is the second replacement. 根据更新后的问题,该问题是第二个替换。 In order to replace multiple strings you will want to do this sequentially: 为了替换多个字符串,您将需要按顺序执行以下操作:

        var original = "AAPC";

        // add arbitrary room to allow for more new characters
        StringBuilder resultString = new StringBuilder(original.Length + 10);
        foreach (char currentChar in original.ToCharArray())
        {
            if (currentChar == 'A') resultString.Append("[A4]");
            else if (currentChar == '4') resultString.Append("[A4]");
            else resultString.Append(currentChar);
        }

        string result = resultString.ToString();

You can run this routine with any replacements you want to make (in this case the letters 'A' and '4' and it should work. If you would want to replace strings the code would be similar in structure but you would need to "look ahead" and probably use a for loop. Hopefully this helps! 您可以使用您要进行的任何替换来运行此例程(在这种情况下,字母'A'和'4'应该可以使用。如果您要替换字符串,则代码的结构类似,但是您需要“向前看”,并可能使用for循环。希望这会有所帮助!

By the way - you want to use a string builder here and not strings because strings are static which means space gets allocated every time you loop. 顺便说一句-您想在这里使用字符串构建器而不是字符串,因为字符串是静态的,这意味着每次循环时都会分配空间。 (Not good!) (不好!)

I think this should do the trick 我认为这应该可以解决问题

string str = "AA4PC";

string result = Regex.Replace(str, @"(?<Before>[^A4]?)(?<Value>A|4)(?<After>[^A4]?)", (m) =>
{
    string before = m.Groups["Before"].Value;
    string after = m.Groups["After"].Value;
    string value = m.Groups["Value"].Value;

    if (before != "[" || after != "]")
    {
        return "[A4]";
    }

    return m.ToString();
});

It is going to replace A and 4 that hasn't been replaced yet for [A4] . 它将替换[A4]尚未替换的A4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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