c# - 使用 C# 查找和替换字符串中的文本

标签 c# string replace

有人知道我如何查找和替换字符串中的文本吗?基本上我有两个字符串:

string firstS = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABQODxIPDRQSERIXFhQYHzMhHxwcHz8tLyUzSkFOTUlBSEZSXHZkUldvWEZIZoxob3p9hIWET2ORm4+AmnaBhH//2wBDARYXFx8bHzwhITx/VEhUf39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f3//";

string secondS = "abcdefg2wBDABQODxIPDRQSERIXFh/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/abcdefg";

我想搜索 firstS 以查看它是否包含 secondS 中的任何字符序列,然后替换它。还需要用方括号中替换字符的个数来替换:

[NUMBER-OF-CHARACTERS-REPLACED]

例如,因为firstSsecondS都包含"2wBDABQODxIPDRQSERIXFh"和"/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/f39/"它们需要更换。那么 firstS 就变成了:

string firstS = "/9j/4AAQSkZJRgABAQEAYABgAAD/[22]QYHzMhHxwcHz8tLyUzSkFOTUlBSEZSXHZkUldvWEZIZoxob3p9hIWET2ORm4+AmnaBhH//2wBDARYXFx8bHzwhITx/VEhUf39[61]f3//";

希望这是有道理的。我想我可以用 Regex 做到这一点,但我不喜欢它的低效率。有人知道另一种更快的方法吗?

最佳答案

Does anyone know of another, faster way?

是的,这个问题实际上有一个专有名称。它被称为 Longest Common Substring , 它有一个 reasonably fast solution .

这里是 an implementation on ideone .它查找并替换十个或更长字符的所有常见子字符串。

// This comes straight from Wikipedia article linked above:
private static string FindLcs(string s, string t) {
    var L = new int[s.Length, t.Length];
    var z = 0;
    var ret = new StringBuilder();
    for (var i = 0 ; i != s.Length ; i++) {
        for (var j = 0 ; j != t.Length ; j++) {
            if (s[i] == t[j]) {
                if (i == 0 || j == 0) {
                    L[i,j] = 1;
                } else {
                    L[i,j] = L[i-1,j-1] + 1;
                }
                if (L[i,j] > z) {
                    z = L[i,j];
                    ret = new StringBuilder();
                }
                if (L[i,j] == z) {
                    ret.Append(s.Substring( i-z+1, z));
                }
            } else {
                L[i,j]=0;
            }
        }
    }
    return ret.ToString();
}
// With the LCS in hand, building the answer is easy
public static string CutLcs(string s, string t) {
    for (;;) {
        var lcs = FindLcs(s, t);
        if (lcs.Length < 10) break;
        s = s.Replace(lcs, string.Format("[{0}]", lcs.Length));
    }
    return s;
}

关于c# - 使用 C# 查找和替换字符串中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169496/

相关文章:

c# - 如何使用 Xamarin Android 访问 ServiceManager

c# - Linq 查询优化

java - 复选框标签到字符串

ruby 正则表达式 : split string with match beginning with either a newline or the start of the string?

excel - Selection.Replace ("."for "-") 应用于 "dd.mm.yyyy"返回 "mm-dd-yyyy"

c# - 在 ASP.net MVC 中显示没有 Controller 或操作的文件夹中的 View

c# - MS Access C#.NET 中的 SQL Server

c - 使用 Strtok 存储字符串片段

loops - 使用 Ansible 替换匹配文件中的几个单词

python - python中 'o'语句去掉第二个 'helloworld'