-4

次の行を含むテキスト ファイルがあります - a boy is drinking water. このファイルを A.txt と呼ぶとします

私の目的は、単語water をユーザーが A.txt に入力したものに置き換えることです。他のものをそのまま保持します。A.txt が変更されa boy is drinking juice、A.txt がパラメーターとして Python スクリプトに渡された場合、出力をa boy is drinking juice.

文字列内の特定の文字だけを置き換えるにはどうすればよいでしょうか。

4

2 に答える 2

4

ファイルを開き、内容を取得します。
単語内の特定の文字列を置き換えるには、textFromFile.replace('oldtext','newtext')
[内容をファイルに保存] を使用します。

于 2013-03-25T09:22:39.847 に答える
-1

このコードを使用してください

namespace string_replace
{
class Program
{
    static void Main(string[] args)
    {
        string text = System.IO.File.ReadAllText(@"a.txt");
        //read the text to replace
        text.Replace("water","user_string");
        System.IO.File.WriteAllText(@"A.txt", text);
    }
  }
}
于 2013-03-25T09:35:16.327 に答える