⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutils.java

📁 一套完整的工商12315的源程序jsp部分在12315里,后台JAVA部分在gs12315src里,没有打包数据库.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        char [] randBuffer = new char[length];
        for (int i=0; i<randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
        }
        return new String(randBuffer);
    }

   /**
    * Intelligently chops a String at a word boundary (whitespace) that occurs
    * at the specified index in the argument or before. However, if there is a
    * newline character before <code>length</code>, the String will be chopped
    * there. If no newline or whitespace is found in <code>string</code> up to
    * the index <code>length</code>, the String will chopped at <code>length</code>.
    * <p>
    * For example, chopAtWord("This is a nice String", 10) will return
    * "This is a" which is the first word boundary less than or equal to 10
    * characters into the original String.
    *
    * @param string the String to chop.
    * @param length the index in <code>string</code> to start looking for a
    *       whitespace boundary at.
    * @return a substring of <code>string</code> whose length is less than or
    *       equal to <code>length</code>, and that is chopped at whitespace.
    */
    public static final String chopAtWord(String string, int length) {
        if (string == null) {
            return string;
        }

        char [] charArray = string.toCharArray();
        int sLength = string.length();
        if (length < sLength) {
            sLength = length;
        }

        //First check if there is a newline character before length; if so,
        //chop word there.
        for (int i=0; i<sLength-1; i++) {
            //Windows
            if (charArray[i] == '\r' && charArray[i+1] == '\n') {
                return string.substring(0, i);
            }
            //Unix
            else if (charArray[i] == '\n') {
                return string.substring(0, i);
            }
        }
        //Also check boundary case of Unix newline
        if (charArray[sLength-1] == '\n') {
            return string.substring(0, sLength-1);
        }

        //Done checking for newline, now see if the total string is less than
        //the specified chop point.
        if (string.length() < length) {
            return string;
        }

        //No newline, so chop at the first whitespace.
        for (int i = length-1; i > 0; i--) {
            if (charArray[i] == ' ') {
                return string.substring(0, i).trim();
            }
        }

        //Did not find word boundary so return original String chopped at
        //specified length.
        return string.substring(0, length);
    }

    /**
     * Highlights words in a string. Words matching ignores case. The actual
     * higlighting method is specified with the start and end higlight tags.
     * Those might be beginning and ending HTML bold tags, or anything else.
     *
     * @param string the String to highlight words in.
     * @param words an array of words that should be highlighted in the string.
     * @param startHighlight the tag that should be inserted to start highlighting.
     * @param endHighlight the tag that should be inserted to end highlighting.
     * @return a new String with the specified words highlighted.
     */
    public static final String highlightWords(String string, String[] words,
        String startHighlight, String endHighlight)
    {
        if (string == null || words == null ||
                startHighlight == null || endHighlight == null)
        {
            return null;
        }

        //Iterate through each word.
        for (int x=0; x<words.length; x++) {
            //we want to ignore case.
            String lcString = string.toLowerCase();
            //using a char [] is more efficient
            char [] string2 = string.toCharArray();
            String word = words[x].toLowerCase();

            //perform specialized replace logic
            int i=0;
            if ( ( i=lcString.indexOf( word, i ) ) >= 0 ) {
                int oLength = word.length();
                StringBuffer buf = new StringBuffer(string2.length);

                //we only want to highlight distinct words and not parts of
                //larger words. The method used below mostly solves this. There
                //are a few cases where it doesn't, but it's close enough.
                boolean startSpace = false;
                char startChar = ' ';
                if (i-1 > 0) {
                    startChar = string2[i-1];
                    if (!Character.isLetter(startChar)) {
                        startSpace = true;
                    }
                }
                boolean endSpace = false;
                char endChar = ' ';
                if (i+oLength<string2.length) {
                    endChar = string2[i+oLength];
                    if (!Character.isLetter(endChar))  {
                        endSpace = true;
                    }
                }
                if ((startSpace && endSpace) || (i==0 && endSpace)) {
                    buf.append(string2, 0, i);
                    if (startSpace && startChar==' ') { buf.append(startChar); }
                    buf.append(startHighlight);
                    buf.append(string2, i, oLength).append(endHighlight);
                    if (endSpace && endChar==' ') { buf.append(endChar); }
                }
                else {
                    buf.append(string2, 0, i);
                    buf.append(string2, i, oLength);
                }

                i += oLength;
                int j = i;
                while( ( i=lcString.indexOf( word, i ) ) > 0 ) {
                    startSpace = false;
                    startChar = string2[i-1];
                    if (!Character.isLetter(startChar)) {
                        startSpace = true;
                    }

                    endSpace = false;
                    if (i+oLength<string2.length) {
                        endChar = string2[i+oLength];
                        if (!Character.isLetter(endChar))  {
                            endSpace = true;
                        }
                    }
                    if ((startSpace && endSpace) || i+oLength==string2.length) {
                        buf.append(string2, j, i-j);
                        if (startSpace && startChar==' ') { buf.append(startChar); }
                        buf.append(startHighlight);
                        buf.append(string2, i, oLength).append(endHighlight);
                        if (endSpace && endChar==' ') { buf.append(endChar); }
                    }
                    else {
                        buf.append(string2, j, i-j);
                        buf.append(string2, i, oLength);
                    }
                    i += oLength;
                    j = i;
                }
                buf.append(string2, j, string2.length - j);
                string = buf.toString();
            }
        }
        return string;
    }

    /**
     * Escapes all necessary characters in the String so that it can be used
     * in an XML doc.
     *
     * @param string the string to escape.
     * @return the string with appropriate characters escaped.
     */
    public static final String escapeForXML(String string) {
        //Check if the string is null or zero length -- if so, return
        //what was sent in.
        if (string == null || string.length() == 0 ) {
            return string;
        }
        char [] sArray = string.toCharArray();
        StringBuffer buf = new StringBuffer(sArray.length);
        char ch;
        for (int i=0; i<sArray.length; i++) {
            ch = sArray[i];
            if(ch == '<') {
                buf.append("&lt;");
            }
            else if (ch == '&') {
                buf.append("&amp;");
            }
            else {
                buf.append(ch);
            }
        }
        return buf.toString();
    }

    public static final String deleteWhiteSpace( String string)
    {
        if (string == null || string.length() == 0 ) {
            return string;
        }
        char [] sArray = string.toCharArray();
        StringBuffer buf = new StringBuffer(sArray.length);
        char ch;
        for (int i=0; i<sArray.length; i++) {
            ch = sArray[i];
            if((ch != '\u0020') && (ch != '\u0009')) {
                buf.append(ch);
            }
        }
        return buf.toString();
    }

    /**
     *    Re-coding the orginal string into a new String
     *    using ISO8859-1, useful with Chinese Charactors
     *    @param string oringinal string
     */
    public static final String getChnString( String string)
    {
          char c;
          StringBuffer buf = new StringBuffer();

        try
           {
                byte[] temp_t = string.getBytes("ISO8859-1");
                String temp = new String(temp_t);
           for(int i = 0;i < temp.length();i++)
           {
                c = temp.charAt(i);
                if(c=='\'')
                {
                    buf.append('‘');
                }
                 if(c=='\"')
                {
                    buf.append('“');
                }
                if(c!='\''&&c!='\"')
                {
                    buf.append(c);
                }
               // if()
           }

              return buf.toString();
           }
           catch(Exception e){
                return "null";
           }

    }
    public static final String getChnString_ms( String string)
    {
           try
           {
                byte[] temp_t = string.getBytes("ISO8859-1");
                String temp = new String(temp_t);
                return temp;
           }
           catch(Exception e){
                return "null";
           }
    }
    public static final String dumpNextLine( String string,int chNum)
    {
          	String chStr = string;
               if(string==null||chNum==0)
               {
               		return "";
               	}
               StringTokenizer sTk = new StringTokenizer(chStr,"<br>");
               String endStr = "";
               String tStr = "";
               while(sTk.hasMoreTokens())
		   {
		   	   tStr = sTk.nextToken();
		   	   if(tStr.length() < chNum)
		   	   {
		   	   	endStr = endStr + tStr +"<br>";
		   	   }
		   	   if(tStr.length() > chNum)
		   	   {
		   	   	endStr = endStr + tStr.substring(0,chNum)+"<br>";
		   	   }
		   	   int k = 0;
		   	   int tK = 1;
		   	   for(k = 0;k < tStr.length();k++)
		   	   {
		   	   	if(k%chNum==0&&k!=0&&k+chNum<=tStr.length())
		   	   	{
		   	   		endStr = endStr + tStr.substring(k,k+chNum)+"<br>";
		   	   		tK++;
		   	   	}
		   	   }
		   	   if(tStr.length() - tK*chNum > 0)
		   	   {
		   	   	endStr = endStr + tStr.substring(tK*chNum);
		   	   }
	   	   }
		return endStr;
    }
    public static final String great_summary(int charNum,String detail)
    {
        htmlSubstring mySubstr = new htmlSubstring();
      	String resultStr = mySubstr.htmlSubstr(detail,charNum);
      	return resultStr;
    }
    public static final String great_summary_3(int charNum,String detail)
    {
       StringBuffer detail_temp = new StringBuffer();
        int len  = 0;
        int pos  = 0;
        int pos2 = 0;
        int pos3 = 0;
        int num  =0;
        int j    = 0;
        char ch;
        String tagStr  = "";
        String tagStr2 = "";
        String tmp     = "";
	String tabStr1 ="<table";
	String tabStr2 ="</table";
	String tdStr1 ="<td";
	String tdStr2 ="</td";
	String trStr1 ="<tr";
	String trStr2 ="</tr";
	String thStr1 ="<th";
	String thStr2 ="<th";
        if(detail != null)
        {
			detail = detail+" ";
			len = detail.length();
            for(int i = 0; i < len; i++)
            {
				ch = detail.charAt(i);
				while (ch == '<')
				{
					pos = detail.indexOf('>',i);
					if(pos>i)
					{
						tagStr = detail.substring(i,pos+1);
						if(tagStr.trim().indexOf(tabStr1)==-1&&tagStr.trim().indexOf(tabStr2)==-1&&tagStr.trim().indexOf(tdStr1)==-1&&tagStr.trim().indexOf(tdStr2)==-1&&tagStr.trim().indexOf(trStr1)==-1&&tagStr.trim().indexOf(trStr2)==-1&&tagStr.trim().indexOf(thStr1)==-1&&tagStr.trim().indexOf(thStr2)==-1)
						{
							detail_temp.append(tagStr);
						}
						if(pos < len - 1)
						{
						    i  = pos + 1;
						    ch = detail.charAt(i);

						    if(j < charNum)
						    {
								pos2 = detail.indexOf("</",i); /* 继续查找匹配的结束Tag,如</FONT> */
								if(pos2 > i) /* 存在匹配的结束Tag */
								{
								    pos3 = detail.indexOf('>',pos2);
								    if(pos3 > pos2)
								    {
								    	tagStr2 = detail.substring(pos2, pos3+1);

								    }

								    if(pos2 - pos > charNum - j) /* 超过剩下的字符数量 */
								    {
										num = pos2 - pos - charNum + j;
										tmp = detail.substring(i, i + num + 1);
										detail_temp.append(tmp);
										if(tagStr2.trim().indexOf(tabStr1)==-1&&tagStr2.trim().indexOf(tabStr2)==-1&&tagStr2.trim().indexOf(tdStr1)==-1&&tagStr2.trim().indexOf(tdStr2)==-1&&tagStr2.trim().indexOf(trStr1)==-1&&tagStr2.trim().indexOf(trStr2)==-1&&tagStr2.trim().indexOf(thStr1)==-1&&tagStr2.trim().indexOf(thStr2)==-1)
										{
											detail_temp.append(tagStr2);
										}
										return detail_temp.toString();
								    }
								}

							}
						}

				    }
				    else /* pos <= i */
				    {
						break;
					}
				} /* end while */

				j++;
                detail_temp.append(ch);
                if(j <= charNum)
                    continue;
                detail_temp.append("\u2026\u2026");
                break;
            }

            return detail_temp.toString();
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -