我在互聯網上尋找修剪字串部分的最快方法。這是輸入:
Hello /*test*/World!
這是我想要達到的結果:
Hello World!
我嘗試使用 String.Remove 執行此操作,但沒有成功。
string input = "Hello /*test*/World!";
string output = input;
int index = output.LastIndexOf("/*");
int index2 = output.LastIndexOf("*/");
if (index >= 0)
{
output = output.Remove(index, index2-3);
}
謝謝!
uj5u.com熱心網友回復:
你可以試試正則運算式:
using System.Text.RegularExpressions;
...
string input = "Hello /*test*/World!";
// we replace each /* ... */ match with empty string
string output = Regex.Replace(input, @"/\*.*?\*/", "");
模式/\*.*?\*/解釋:
/\* - /*, note that * is escaped
.*? - any symbols but as few as possible
\*/ - */, note that * is escaped
uj5u.com熱心網友回復:
這是我在史蒂夫的幫助下制定的解決方案。
string input = "Hello /*test*/World!";
string output = input;
int index = output.LastIndexOf("/*");
int index2 = output.LastIndexOf("*/");
if (index >= 0)
{
output = output.Remove(index, index2 2 - index);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/370846.html
