當我通過 CMD 運行我的代碼時,訊息被發送到:
void Main(string[]args)
如果他們在 Visual Studio 上使用 C#,每個人都會這樣做。但是當您從 CMD 運行代碼并鍵入訊息時。看來該引數可以包含以下訊息:
“程式名車”在藍樓“”
(programName 是程式的名稱,它是為執行代碼而鍵入的,并且引數在它之后發送,如上)訊息被發送到 main 方法中的 args,但“在藍色建筑物中”仍然完全保留在一個引數中. 對于我顯示的訊息,args[0] 將包含“car”,而 args[1] 將包含“is in blue building”。
知道這一點的主要目的是因為我試圖在 Windows 表單中復制界面和行程,我不確定如何將我在引號中發送的字串單獨放入一個引數中(如圖 args[1] 所示)。
我試過這個(讓變數“arg”成為字串:
args = textBox1.Text;
if(textBox1.Text.Contains("\""))
{
int m = 0;
string quote = null;
string[] tmp = textBox1.Text.Split(' ');
for (int i = 0; i < tmp.Length; i )
{
if(tmp[i].Contains("\'"))
{
m ;
}
if (m == 1)
{
quote = tmp[i];
}
string quote1 = textBox1.Text.Replace(quote, "").Replace("\"", "");
string[] tmp1 = quote1.Split(' ');
quote = "'" quote "'";
tmp1 = quote.Split(' ');
}
however I am still working on it. but without any error, it will still be difficult to send it back to the main as it wants to be converted to args..
uj5u.com熱心網友回復:
您可以將參考的文本視為一個字串,而無需使用 Split 函式,而是使用 Regex。以下面的代碼片段為例:
// In your case just read from the textBox for input
string input = "cars \"testing string\"";
// This code will split the input string along spaces,
// while keeping quoted strings together
string[] tmp = Regex.Split(input,
"(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
// Now must remove the quotes from the Regex'd string
string[] args = tmp.Select(str => str.Replace("\"", "")).ToArray();
// Now when printing the arguments, they are correctly split
for (int i = 0; i < args.Length; i )
{
Console.WriteLine("args[" i "]:'" args[i] "'");
}
我在這個鏈接上找到了你需要的特定正則運算式字串,這里是堆疊溢位。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449660.html
