Programming issue at work place related to string
<p>I have a string which is searched based on the delimiter <code>"</code> (double quote).</p>

<p>So when I enter the string <code>"program"</code>, it is able to search the beginning and end of the string based on the delimiter and returns me the string program which I put in a vector.</p>

<p>Now, if I enter a string <code>"program"123""</code> it returns me substrings like <code>program</code>, <code>123</code>, <code>123"</code>. </p>

<p>Now the result I want is <code>program"123"</code> which is a valid string as per the usecase but it contains <code>"</code> as part of the string and this is where the search by delimiter fails to distinguish between the beginning and end of the string.</p>

<p>Can someone help with some logic?</p>

<p>The following is the method I am using.</p>

<pre><code>enter code here
</code></pre>

<p>public static PVector tokenizeInput(final String sCmd)
            throws ExceptionOpenQuotedString
    {
        if (sCmd == null)
        {
            return null;
        }</p>

<pre><code>    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 &lt; 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 &amp;&amp; !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 &lt; nLength; i++)

    if (!bInWhitespace &amp;&amp; start &lt; nLength)
    {
        if (!bInQuotedToken || chCurrent == '"')
        {
            rc.add(sCmd.substring(start));
        }
        else
        {
            throw new ExceptionOpenQuotedString(sCmd.substring(start));
        } // else - if (!bInQuotedToken)
    } // if (!bInWhitespace &amp;&amp; start &lt; nLength)
    return rc;
}
</code></pre>
