【问题标题】:Java substring is switching my positive indexes with negative onesJava 子字符串正在将我的正索引与负索引切换
【发布时间】:2011-12-03 12:39:36
【问题描述】:

我得出的结论是启用 HTML 的 JTextPanes 不支持自动换行。所以我试图提供一种家庭酿造方法。完成后我会在网上发布。我可能没有最好的技术,但它应该在完成后工作。我的问题是出于某种疯狂(非常令人沮丧)的原因,当我将索引传递给我的子字符串命令时,它会将实际值切换为相同的值但为负值并抛出 java.lang.StringIndexOutOfBoundsException。但是当变量被发送到子字符串命令时,它肯定是肯定的。当我用变量替换值时,它工作正常。我将不胜感激任何帮助。我将包括酸味。

    String wordWrap( String oldtxt )    {

        int ishere = 0;         // the current index
        int charlen = 0;        // The current length of the current line
        int beginint = 0;       // Where the text between tags begins
        int endint = 0;         // Where the text between tags ends
        String tempstr = "";    // Where the text is 
        String newoldtxt = "";  // to work around the damned oc error
        String newtxt = "";     // The new text being built
        String divystr = "";    // Temp variable to hold a partial string
        Boolean a = true;       // Needed as temp storage variable

        newoldtxt = oldtxt;

        while( true ) {

            endint = oldtxt.indexOf( "<", endint );

            if( endint == -1 )  {

                endint = oldtxt.length();
                a = false;
            }


            ishere = endint;
            tempstr = oldtxt.substring( beginint, endint );             // Save the text in a temp string

            while( ishere > endint )
            if( tempstr.length() > ( 22 - charlen ))  {                 // Testing for a complete line

//              newtxt += tempstr.substring( ishere, 22 - charlen );        // If a line is complete a line is printed to the pane
                newtxt += tempstr.substring( ishere, 22 );      // If a line is complete a line is printed to the pane
                ishere += 22 - charlen;                                     // Bumping the current index along
                charlen = 0;
                newtxt += "<br />";                                         // Attach a line break
                if( ishere >= tempstr.length() && a == false )
                return newtxt;

            } else if( tempstr.length() < ( 22 - charlen) )  {          // Checking to see if there are fewer then 22 chars in the string

                divystr = tempstr.substring( ishere, tempstr.length() );    // Dump the rest of the substring into the rebuilt string
                newtxt += divystr;                                          // Assign the remaining tempstr characters to newtxt
                charlen += divystr.length();                                // Add stray chars to charlen

                if( a == false )
                    return newtxt;
            }

            beginint = oldtxt.indexOf( ">", ( endint ) );                       // Locate end bracke
            newtxt += oldtxt.substring( beginint, endint );             // Add tag to newtxt
        }
    }
}

【问题讨论】:

  • @Roland Sams 写道 do not support word wrapping 我认为这根本不是真的

标签: java html string swing jtextpane


【解决方案1】:

最后一次调用substring时,endint的值为0,大于起始索引,导致StringIndexOutOfBoundsException。

// endint is larger than beginint here
newtxt += oldtxt.substring( beginint, endint );

【讨论】:

  • 是的,谢谢,但我已经尝试过了。我仍然得到越界错误。此代码还不是发布代码。由于这个讨厌的故障,我还没有机会赶出这些错误
  • 你使用什么作为输入数据? endint 的值由oldtxt 中第一个&lt; 登录的位置决定。
  • 顺便说一句:越界异常是 -21,实际索引应该是 21。我确信发送的变量因为索引是 21,由于某种原因子字符串方法反转了它。
  • substring 没有反转它们,尝试在调用substring 之前打印开始和结束索引。您会注意到结束索引小于开始索引。
  • endint 用于检测 HTML 标签的左括号,以便该方法可以正确处理它
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多