在C#中用索引值替换文本的问题

时间:2014-03-20 18:00:22

标签: c#

我有一个文本文件,其中包含一个名为" map"的重复字符串。超过800现在我想用map替换它们map0,map1,map2,..... map800。

我试过这种方式,但它对我没用:

void Main()    {
 string text = File.ReadAllText(@"T:\File1.txt");
   for (int i = 0; i < 2000; i++)
       {
         text = text.Replace("map", "map"+i);
       }
  File.WriteAllText(@"T:\File1.txt", text);
}

你能告诉我怎么做吗?

4 个答案:

答案 0 :(得分:11)

这应该可以正常工作:

void Main() {

    string text = File.ReadAllText(@"T:\File1.txt");
    int num = 0;

    text = (Regex.Replace(text, "map", delegate(Match m) {
        return "map" + num++;
    }));

    File.WriteAllText(@"T:\File1.txt", text);
}

答案 1 :(得分:0)

/// <summary>
/// Replaces each existing key within the original string by adding a number to it.
/// </summary>
/// <param name="original">The original string.</param>
/// <param name="key">The key we are searching for.</param>
/// <param name="offset">The offset of the number we want to start with. The default value is 0.</param>
/// <param name="increment">The increment of the number.</param>
/// <returns>A new string where each key has been extended with a number string with "offset" and beeing incremented with "increment".The default value is 1.</returns>
/// <example>
/// Assuming that we have an original string of "mapmapmapmap" and the key "map" we
/// would get "map0map1map2map3" as result.
/// </example>
public static string AddNumberToKeyInString(string original, string key, int offset = 0, int increment = 1)
{
    if (original.Contains(key))
    {
        int counter = offset;
        int position = 0;
        int index;

        // While we are withing the length of the string and
        // the "key" we are searching for exists at least once
        while (position < original.Length && (index = original.Substring(position).IndexOf(key)) != -1)
        {
            // Insert the counter after the "key"
            original = original.Insert(position + key.Length, counter.ToString());
            position += index + key.Length + counter.ToString().Length;
            counter += increment;
        }
     }

     return original;
}

答案 2 :(得分:-1)

这是因为您每次都要替换相同的地图。因此,如果原始字符串为&#34; map map map&#34;,则生成的字符串将具有map9876543210 map9876543210 map9876543210 10次迭代。您需要找到每个单独的地图,并替换它。尝试使用indexof方法。

答案 3 :(得分:-1)

这些方面的某些内容可以让您了解您正在尝试做的事情:

static void Main(string[] args)
    {
        string text = File.ReadAllText(@"C:\temp\map.txt");
        int mapIndex = text.IndexOf("map");
        int hitCount = 0;
        int hitTextLength = 1;
        while (mapIndex >= 0 )
        {
            text = text.Substring(0, mapIndex) + "map" + hitCount++.ToString() + text.Substring(mapIndex + 2 + hitTextLength);
            mapIndex = text.IndexOf("map", mapIndex + 3 + hitTextLength);
            hitTextLength = hitCount.ToString().Length;
        }
        File.WriteAllText(@"C:\temp\map1.txt", text);
    }

由于字符串是不可变的,因此这不是处理大文件(1MB +)的理想方式,因为您要为文件中的每个“map”实例创建和处理整个字符串。

对于示例文件:

map hat dog
dog map cat

lost cat map

mapmapmaphat

map

你得到了结果:

map0 hat dog
dog map1 cat

lost cat map2

map3map4map5hat

map6