0

文字列の例は次のとおりです。

/my/test-is/for-this/to/work-right.txt

次のようにする必要があります。

/my/test_is/for_this/to/work-right.txt

Regex-Fu の筋肉を鍛えたい人はいますか?

4

1 に答える 1

4

いいえ、そうではありません。Path.GetDirectoryNameと友達を使用する方がはるかに優れています:

var s = "/my/test-is/for-this/to/work-right.txt";
var result = Path.Combine(
     Path.GetDirectoryName(s).Replace("-", "_"),
     Path.GetFileName(s)
);

パス/がディレクトリ セパレーターとして使用されている場合、Windows で実行していて、置き換えたくない場合は\、後で置き換えを元に戻すことができます。

// What I 'm really doing here is showing that these constants exist;
// read the manual to see what you can expect from them.
result = result.Replace(
    Path.DirectorySeparatorChar,
    Path.AltDirectorySeparatorChar
);
于 2013-07-12T23:13:28.847 に答える