我有一個字符串,它是根據分隔符"
(雙引號)進行搜索的。與字符串有關的工作地點編程問題
所以當我輸入字符串"program"
時,它能夠根據分隔符搜索字符串的開始和結尾,並返回我放入向量中的字符串程序。
現在,如果我輸入字符串"program"123""
,它會返回像我這樣的子字符串,如program
,123
,123"
。
現在的結果我想是program"123"
這是一個有效的字符串作爲每一個用例,但它包含"
作爲字符串的一部分,這就是由分隔符搜索失敗字符串的開頭和結尾來區分。
有人可以幫助一些邏輯?
以下是我正在使用的方法。
enter code here
公共靜態PVector tokenizeInput(最後絃樂SCMD) 拋出ExceptionOpenQuotedString { 如果(SCMD == NULL){ 回報 空; }
PVector rc = new PVector();
if (sCmd.length() == 0)
{
rc.add(StringTable.STRING_EMPTY);
return rc;
}
char chCurrent = '\0';
boolean bInWhitespace = true;
boolean bInQuotedToken = false;
boolean bDelim;
int start = 0;
int nLength = sCmd.length();
for (int i = 0; i < nLength; i++)
{
chCurrent = sCmd.charAt(i); // "abcd "ef"" rtns abdc ef ef"
bDelim = -1 != APIParseConstants.CMD_LINE_DELIMS.indexOf(chCurrent);
if (bInWhitespace) // true
{
// In whitespace
if (bDelim)
{
if ('\"' == chCurrent)
{
start = i + 1;
bInQuotedToken = true;
bInWhitespace = false;
} // if ('\"' == chCurrent)
}
else
{
start = i;
bInWhitespace = false;
} // else - if (bDelim)
}
else
{
// Not in whitespace
boolean bAtEnd = i + 1 == nLength;
if (!bDelim)
{
continue;
}
else
{
if ('\"' == chCurrent)
{
if (!bInQuotedToken)
{
// ending current token due to '"'
if (bAtEnd)
{
// non terminated quoted string at end...
throw new ExceptionOpenQuotedString(
sCmd.substring(start));
}
else
{
rc.add(sCmd.substring(start, i)); // include quote
bInQuotedToken = true;
bInWhitespace = false;
} // if (bAtEnd)
}
else
{
// ending quoted string
//if (!bAtEnd)
{
rc.add(sCmd.substring(start, i)); // don't include quote
bInQuotedToken = false;
bInWhitespace = true;
} // if (bAtEnd)
} // else - if (!bInQuotedToken)
}
else
{
// got delim (not '"')
if (!bAtEnd && !bInQuotedToken)
{
rc.add(sCmd.substring(start, i));
bInWhitespace = true;
} // if (bAtEnd)
} // else - if ('\"' == chCurrent)
} // else - if (!bDelim)
} // else - if (bInWhitespace)
} // for (short i = 0; i < nLength; i++)
if (!bInWhitespace && start < nLength)
{
if (!bInQuotedToken || chCurrent == '"')
{
rc.add(sCmd.substring(start));
}
else
{
throw new ExceptionOpenQuotedString(sCmd.substring(start));
} // else - if (!bInQuotedToken)
} // if (!bInWhitespace && start < nLength)
return rc;
}
如何區分雙引號作爲分隔符和字符串的一部分?我不是在要求你的代碼,而是在純英文的規則。 –
你真的應該向我們展示你在字符串中讀取的代碼並標記它。 – Wernsey
正則表達式是你的朋友。 – mah