String paging code

  Abstract: string paging code 

  </ Td> </ tr> <tr> <td width="527" height="35" valign="top" class="ArticleTeitle"> use examples: 
  StringPager pager = new StringPager (); 
  Pager.setSizePerPage (5); / / characters per page 
  Pager.setText ( "12345678901 ");// need a long text paging 
  Pager.setCurrentPage (2); / / Current Page 

  System.out.println (pager.getTotalPage ());// total number of pages 
  System.out.println (pager.getCurrentPagedText pages of the current string ());// 

  Note: Java-based concept of the length of the string, each page is displayed in English and Chinese, as are the length of a page. 
  All source documents: 


  Package examples; 
  / * 
  * @ (#) StringPager.java 1.00 2004-10-10 
*
  * Copyright 2004 All rights reserved. 
  * PROPRIETARY / CONFIDENTIAL. Use is subject to license terms. 
  * / 


  / ** 
  * StringPager, string tabs. 
*
  * @ Author Liuchangjiong 
  * @ Version 1.00 2004-10-10 
  * / 
  (Public class StringPager 

  / ** Current page number * / 
  Private int currentPage = 1; 

  / ** Paging needs of the long string * / 
  Private String text; 

  / ** Characters per page, the default is 200 * / 
  Private int sizePerPage = 200; 

  / ** Total number of pages * / 
  Private int totalPage; 


  Public StringPager () () 

  / ** 
  * Return to the pages of string. 
  * @ Return 
  * / 
  Public String getCurrentPagedText () ( 
  Try ( 
  If (getCurrentPage () <getTotalPage ()) ( 
  Return getText (). Substring ((getCurrentPage () - 1) * getSizePerPage (), 
  GetCurrentPage () * getSizePerPage ()); 
  ) Else if (getTotalPage ()> 0) ( 
  Return getText (). Substring ((getCurrentPage () - 1) * getSizePerPage ()); 
  ) 
  ) Catch (Exception e) ( 
  E.printStackTrace (); 
  ) 
  Return null; 
  ) 

  / ** 
  * @ Return Returns the current page number. 
  * / 
  Public int getCurrentPage () ( 
  If (currentPage <= 0) currentPage = 1; 

  Return currentPage; 
  ) 

  / ** 
  * Set up the current page, from the beginning. 
*
  * @ Param currentPage 
  * The number to set the current page number. 
  * / 
  Public void setCurrentPage (int currentPage) ( 
  If (currentPage <= 0) ( 
  CurrentPage = 1; 
  ) 
  This.currentPage = currentPage; 
  ) 

  / ** 
  * @ Return Returns the total number of pages, if there is no data on a return. 
  * / 
  Public int getTotalPage () ( 
  If (getText () == null) totalPage = 0; 
  TotalPage = (int) Math.ceil (1.0 * getText (). Length () / getSizePerPage ()) / / total number of pages 

  If (totalPage == 0) 
  TotalPage = 1; 
  Return totalPage; 
  ) 

  / ** 
  * @ Param totalPage 
  * The totalPage to set. 
  * / 
  Public void setTotalPage (int totalPage) ( 
  This.totalPage = totalPage; 
  ) 

  Public static void main (String [] args) ( 
  StringPager pager = new StringPager (); 
  Pager.setSizePerPage (5); 
  Pager.setText ( "12345678901"); 
  Pager.setCurrentPage (2); 

  System.out.println (pager.getTotalPage ()); 
  System.out.println (pager.getCurrentPagedText ()); 
  ) 

  / ** 
  * @ Return Returns the number of characters per page. 
  * / 
  Public int getSizePerPage () ( 
  Return sizePerPage; 
  ) 

  / ** 
  * @ Param sizePerPage 
  * The number of characters per page to set. 
  * / 
  Public void setSizePerPage (int sizePerPage) ( 
  This.sizePerPage = sizePerPage; 
  ) 

  / ** 
  * @ Return Returns the paging needs of the long string. 
  * / 
  Public String getText () ( 
  Return text; 
  ) 

  / ** 
  * @ Param text 
  * The need for paging long strings to set. 
  * / 
  Public void setText (String text) ( 
  This.text = text; 
  ) 
  ) 

  Below are the jsp call: testpage.jsp (download) 
  </ Td> <td width="175" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

URL string to increase hyperlinks

  Abstract: URL string to increase hyperlinks 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="738" border="0"> <tr> <td width = " 732 "> </ td> </ tr> </ table> 
  Read in the form data, and if we want to filter users to the text, to which the url string add hyperlinks, then the following code so that we can easily achieve this function, the code via jive1.4 FilterURLConverter.java, head of Finishing. 
  Package example; 
  (Public class FilterURLConverter 
  Private String convertURL (String input) ( 
  / / Check if the string is null or zero length - if so, return 
  / / What was sent in. 
  If (input == null | | input.length () == 0) ( 
  Return input; 
  ) 
  Else ( 
  StringBuffer buf = new StringBuffer (); 

  Int i = 0, j = 0, oldend = 0; 
  Int len = input.length (); 
  Char cur; 

  While ((i = input.indexOf ( "http://" oldend))> = 0) ( 
  J = i +7; 
  Cur = input.charAt (j); 
  While (j <len) ( 
  / / Is a space? 
  If (cur == '') break; 
  / / Is html? 
  If (cur == '<') break; 
  / / Is a Win32 newline? 
  If (cur == '\ n') break; 
  / / Is Unix newline? 
  If (cur == '\ r' & & j    Break; 

  J + +; 
  If (j    Cur = input.charAt (j); 
  ) 
  ) 
  Buf.append (input.substring (oldend, i)); 
  Buf.append ( "buf.append (input.substring (i, j)); 
  Buf.append ( "\ ">"); 
  Buf.append (input.substring (i, j)); 
  Buf.append (""); 
  Oldend = j; 
  ) 
  Buf.append (input.substring (j, len)); 
  Return buf.toString (); 
  ) 
  ) 
  Public static void main (String args []){// test 
  String s = "my school home is http://10.10.l.1 OK OK"; 
  FilterURLConverter f = new FilterURLConverter (); 
  System.out.println (f.convertURL (s)); 

  ) 


  ) 
  The result: 
  C: \ java> javac FilterURLConverter.java 
  C: \ java> java FilterURLConverter 
  My school home is http://10.10.l.1 OK OK 

  Jsp in this test can be: 
  <% @ Page import = "example .*"%> 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 

<head>


  </ Head> 
<body>
  <form Name="form1" method="post" action=""> 
  Your Home URL address: 
  <input Type="text" name="url"> 
  <input Type="submit" name="Submit" value="提交"> 
  </ Form> 
<%
  FilterURLConverter f = new FilterURLConverter (); 
  String url = request.getParameter ( "url"); 
  If (url! = Null) ( 
  Url = new String (url.getBytes ( "ISO8859_1")); 
  String filterurl = f.convertURL (url); 
  Out.println ( "Your home is:" + filterurl); 
  ) 

%>
  </ Body> 
  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

In a string of gaps and remove newline

  Abstract: In a string of gaps and remove newline 

  </ Td> </ tr> <tr> <td width="467" height="35" valign="top" class="ArticleTeitle"> 
  / / Cut off in a string of "gaps and newline." 
  (Public class ChopWord 
  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    / / Windows 
  If (charArray [i] == '\ r' & charArray [i +1] == '\ n') ( 
  Return string.substring (0, i +1); 
  ) 
  / / 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); 
  ) 
  Public static void main (String args []) ( 
  String s1 = "abcdefg"; 
  String s2 = "abc defg"; 
  String s3 = "abcdefg \ nijk"; 
  String s4 = "abc \ r \ ndddd"; 
  String s5 = "ADDDDSC \ nkklss \ nddd \ n"; 
  System.out.println (chopAtWord (s1, 6)); 
  System.out.println (chopAtWord (s2, 6)); 
  System.out.println (chopAtWord (s3, 6)); 
  System.out.println (chopAtWord (s4, 6)); 
  System.out.println (chopAtWord (s5, 10)); 
  ) 
  ) 


  The result: 

  C: \ java> javac ChopWord.java 

  C: \ java> java ChopWord 
  Abcdef 
  Abc 
  Abcdef 
  Abc 
ADDDDSC
  </ Td> <td width="217" align="center" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

Replacement of a string function

  Abstract: replacement of a string function 

  </ Td> </ tr> <tr> <td width="495" height="35" valign="top" class="ArticleTeitle"> 
  (Public class test 

  Public test () () 

  / / Function replacement string 
  / / String strSource - the source string 
  / / String strFrom - to replace the substring 
  / / String strTo - is replaced by the string 
  Public static String replace (String strSource, String strFrom, String strTo) 
  ( 
  / / If we replace the substring is empty, there is the direct source of the return string 
  If (strFrom == null | | strFrom.equals ("")) 
  Return strSource; 
  String strDest = ""; 
  / / String to replace the length of 
  Int intFromLen = strFrom.length (); 
  Int intPos; 
  / / String replacement cycle 
  While ((intPos = strSource.indexOf (strFrom))! = -1) 
  ( 
  / / Get matched substring of string to the left 
  StrDest strDest + strSource.substring = (0, intPos); 
  / / Replacement with the substring 
  StrDest + = strDest strTo; 
  / / Modify the source string is the string-matching substring 
  StrSource = strSource.substring (intFromLen intPos +); 
  ) 
  / / With no match in the series 
  StrDest + = strDest strSource; 
  / / 
  Return strDest; 
  ) 

  Public static void main (String args []) ( 
  String srcString = "How doo you doo"; 
  String strFrom = "doo"; 
  String strTo = "do"; 
  String strDest = replace (srcString, strFrom, strTo); 
  System.out.println ( "srcString = How doo you doo"); 
  System.out.println ( "strDest =" + strDest); 
  ) 
  ) 
  The result: 

  C: \ java> javac test.java 

  C: \ java> java test 
  SrcString = How doo you doo 
  StrDest = How do you do function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document.getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> <td width="189" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

String of the proper use of attention to the points

  Abstract: The correct use of type String attention to the points 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> 
  <table Width="680" border="0"> <tr> <td width="407"> java.lang.String category of people is the most familiar, we rarely write java procedures do not have the String.    This paper describes how proper use String, mainly covers initialization, and a comparison operation Series. 

  First, we must be clear that String is the final type, so you can not inherit this class, this class can not be changed.    String time we use a very simple, usually String s = "hello", but in the Java API at the same time provide a constructor function for String (String s), so you can also use this String s = new String ( "hello" ), a String initialization behind this approach is not recommended, because the new operators will mean that the target of a new generation, if the operation took place in such a cycle, then the price is heavy.    For example, 

  For (int i = 0; i <1000; i + +) ( 

  String s = new String ( "hello"); 
  ) </ Td> <td width="263" align="center"> </ td> </ tr> </ table> 
  This will create 1,000 String type of object, as the String class is the final, this is in fact the operation of every generation of a new String object.    If you use String s = "hello"; then reuse can be achieved, why can reuse, it will be explained below. 

  When we use "+" to achieve tandem operation, such as the String s = "hello" + "world" is through such append StringBuffer () method to achieve, and the final return to the s String.    If interested, you can write a simple example, and then use javap see how the virtual machine is working.    In the use of the series when we should also pay attention to the final String class, if you need multiple series such as: 
  String sql = "xxx"; 
  Sql = "xxxx"; 
  Sql = "ssssss"; 
  In order to improve efficiency so space-saving, we should use their own StringBuffer to replace "+"; 

  String comparison normally there are two, one is to use ==, and the other is the use of equals () method, the target == attention to the address comparison, and the String of equals () method is the Object class coverage methods, and to achieve targets for the contents of the String comparison.    So String s1 = new String ( "hello"); String s2 = new String ( "hello"), we s1 and s2 comparison to the above-mentioned time, the former should return to false, because of the use of the new generation are two different targets .    The latter should return to true because of their content is the same, is "hello."    Then if we still have a String s3 = "hello"; he and the comparison s1 What should look like this, the answer is as false s1 == s3, compared to the true equals.    In fact String class String is the maintenance of a pool, the pool is empty initialization, and when we String x = "hello", hello Add to this the pool will be, when we again String y = "hello "When he first to check whether there is a pool and hello content of the same object, if any will be back to this quote y, if we do not exist, and Add to the creation of a pool.    Thereby bringing about reuse.    String in a way intern () He can be the object of the String Add to return to the pool and pool-target.    If we s1 (String s1 = new String ( "hello")) call intern, s1 = s1.intern (), we then s1 and s3 "==" judgement, you will find that the results returned to the true! 
  Look at the following example 

  Public class StringTest 
  ( 

  Public static void main (String [] args) 
  ( 
  String s1 = "hello"; 
  String s2 = new String ( "hello"); 
  String s3 = new String ( "hello"); 

  String y = "hello"; 
  System.out.println ( "s1 = y is" + (s1 == y)); 

  TestString (s1, s2, s3); 
  S2 = s2.intern (); 
  System.out.println ( "after s2.intern"); 
  TestString (s1, s2, s3); 



  ) 

  Private static void testString (String s1, String s2, String s3) 
  ( 

  System.out.println ( "s1 = s2 is" + (s1 == s2)); 
  System.out.println ( "s2 = s3 is" + (s2 == s3)); 
  System.out.println ( "s1 = s3 is" + (s1 == s3)); 
  System.out.println ( "s1.equals (s2) is" + s1.equals (s2)); 
  System.out.println ( "s2.equals (s3) is" + s2.equals (s3)); 
  ) 
  ) 
  Output results 

  D: \ java> java StringTest 
  S1 = y is true 
  S1 = s2 is false 
  S2 = s3 is false 
  S1 = s3 is false 
  S1.equals (s2) is true 
  S2.equals (s3) is true 
  After s2.intern 
  S1 = s2 is true 
  S2 = s3 is false 
  S1 = s3 is false 
  S1.equals (s2) is true 
  S2.equals (s3) is true 

  D: \ java> 
  </ Td> </ tr> <tr> 

  ↑ Back 

String replacement

  Abstract: string replacement 

  </ Td> </ tr> <tr> <td width="483" height="35" valign="top" class="ArticleTeitle"> following procedure extracted from the StringUtils.java jive1.24, head of finishing. 

  Public class Replace ( 

  / / This method will be the string line-string replacement for all oldString newString 
  Public static final String replace (String line, String oldString, String newString) 
  ( 
  If (line == null) ( 
  Return null; 
  ) 
  Int i = 0; 
  If ((i = line.indexOf (oldString, i))> = 0) ( 
  Char [] = line2 line.toCharArray (); 
  Char [] = newString2 newString.toCharArray (); 
  Int oLength = oldString.length (); 
  StringBuffer buf = new StringBuffer (line2.length); 
  Buf.append (line2, 0, i). Append (newString2); 
  I + = oLength; 
  Int j = i; 
  While ((i = line.indexOf (oldString, i))> 0) ( 
  Buf.append (line2, j, ij). Append (newString2); 
  I + = oLength; 
  J = i; 
  ) 
  Buf.append (line2, j, line2.length - j); 
  Return buf.toString (); 
  ) 
  Return line; 
  ) 

  Public static void main (String args []) ( 
  String s = "I OK OK best OK"; 
  String s1 = replace (s, "OK", "hello"); 
  System.out.println (s1); 
  ) 
  ) 

  Test results: 

  C: \ java> javac Replace.java 

  C: \ java> java Replace 
  I hello hello best hello 

  C: \ java> </ td> <td width="201" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

Byte array into a hexadecimal string

  Abstract: byte array into a hexadecimal string 

  </ Td> </ tr> <tr> <td width="493" height="35" valign="top" class="ArticleTeitle"> / / code via jdon2.5. 
  (Public class BytetoHex 
  Public static final String encodeHex (byte [] bytes) ( 
  StringBuffer buf = new StringBuffer (bytes.length * 2); 
  Int i; 

  For (i = 0; i <bytes.length; i + +) ( 
  If (((int) bytes [i] & 0xff) <0×10) ( 
  Buf.append ( "0"); 
  ) 
  Buf.append (Long.toString ((int) bytes [i] & 0xff, 16)); 
  ) 
  Return buf.toString (); 
  ) 

  Public static void main (String args []) ( 
  Byte By [] = (34,12,4, -6, -8,55,127,88); 
  System.out.println (encodeHex (By)); 
  ) 
  ) 

  The result: 

  C: \ java> javac BytetoHex.java 

  C: \ java> java BytetoHex 
220c04faf8377f58

  C: \ java> </ td> <td width="191" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

16 hex string into a byte array

  Abstract: 16 hex string into a byte array 

  </ Td> </ tr> <tr> <td width="512" height="35" valign="top" class="ArticleTeitle"> / / code to jive-jdon. 
  (Public class HexstringtoByte 
  Public static final byte [] decodeHex (String hex) ( 
  Char [] = hex.toCharArray chars (); 
  For (int i = 0; i    System.out.print (chars [i]); 
  ) 
  System.out.println (); 
  Byte [] bytes = new byte [chars.length / 2]; 
  Int byteCount = 0; 
  For (int i = 0; i    Byte newByte = 0×00; 
  NewByte | = hexCharToByte (chars [i]); 
  NewByte <<= 4; 
  NewByte | = hexCharToByte (chars [i +1]); 
  Bytes [byteCount] = newByte; 
  ByteCount + +; 
  ) 
  Return bytes; 
  ) 

  Private static final byte hexCharToByte (char ch) ( 
  Switch (ch) ( 
  Case'0 ': return 0×00; 
  Case'1 ': return 0×01; 
  Case'2 ': return 0×02; 
  Case'3 ': return 0×03; 
  Case'4 ': return 0×04; 
  Case'5 ': return 0×05; 
  Case'6 ': return 0×06; 
  Case'7 ': return 0×07; 
  Case'8 ': return 0×08; 
  Case'9 ': return 0×09; 
  Case 'a': return 0×0A; 
  Case 'b': return 0×0B; 
  Case 'c': return 0×0C; 
  Case 'd': return 0×0D; 
  Case 'e': return 0×0E; 
  Case 'f': return 0×0F; 
  ) 
  Return 0×00; 
  ) 

  Public static void main (String args []) ( 
  String s = "220c04faf8377f58"; 
  [] = Byte a decodeHex (s); 
  For (int i = 0; i    System.out.println (a [i]); 
  ) 
  ) 


  The result: 
  C: \ java> java HexstringtoByte 

  C: \ java> java HexstringtoByte 
220c04faf8377f58
34
12
4
-6
-8
55
127
88

  C: \ java> </ td> <td width="172" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

A string Tools

  Abstract: a string Tools 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="671" height="29" border="0"> <tr> <td width="277"> 
  </ Td> <td width="384"> a string Tools 
  Package studio.beansoft.jsp; 

  Import javax.servlet.ServletContext; 
  Import javax.servlet.http .*; 
  Import java.io. *; 

  / ** 
*

  Title: string Tools 

*

  Description: A convenient method of the string instruments. 

*

  Copyright: Copyright (c) 2003 

*

  Company: Oriental century 

  * @ Author Liuchangjiong 
  * @ Version 1.0 
  * / 

  </ Td> </ tr> </ table> 
  (Public class StringUtil 
  Public StringUtil () ( 
  ) 

  / / Internal statement convenient method 

  / / String approach ——– ———————————— ————————————– 
  / / String source of oldStr newStr replacement for, and case-sensitive manner View 
  Public String replace (String source, String oldStr, String newStr) ( 
  Return replace (source, oldStr, newStr, true); 
  ) 

  / / String source replacement for the oldStr newStr, matchCase set for the case-sensitive search 
  Public String replace (String source, String oldStr, String newStr, 
  Boolean matchCase) ( 
  If (source == null) 
  Return null; 
  / / First check whether there is the old string, and there is no replacement not 
  If (source.toLowerCase (). IndexOf (oldStr.toLowerCase ()) == -1) ( 
  Return source; 
  ) 

  Int findStartPos = 0; 
  Int a = 0; 
  While (a> -1) ( 
  Int b = 0; 
  String str1, str2, str3, str4, strA, strB; 
  Str1 = source; 
  Str2 = str1.toLowerCase (); 
  Str3 = oldStr; 
  Str4 = str3.toLowerCase (); 
  If (matchCase) ( 
  StrA = str1; 
  StrB = str3; 
  ) 
  Else ( 
  StrA = str2; 
  StrB = str4; 
  ) 
  A = strA.indexOf (strB, findStartPos); 
  If (a> -1) ( 
  B = oldStr.length (); 
  FindStartPos = a + b; 
  StringBuffer bbuf = new StringBuffer (source); 
  Source = bbuf.replace (a, a + b, newStr) + ""; 
  / / Find a new starting point in the replacement after the end of a string 
  FindStartPos + = findStartPos newStr.length () - b; 
  ) 
  ) 
  Return source; 
  ) 

  / / Remove the danger of posting HTML code, mainly script code, and rolling subtitles code, as well as dealing with the incident script code 
  Public String replaceHtmlCode (String content) ( 
  If (isEmpty (content)) 
  Return ""; 
  / / Scripts incident keyword filter 
  String [] = (eventKeywords 
  "Onmouseover" 
  "Onmouseout" 
  "Onmousedown" 
  "Onmouseup" 
  "Onmousemove" 
  "Onclick" 
  "Ondblclick" 
  "Onkeypress" 
  "Onkeydown" 
  "Onkeyup" 
  "Ondragstart" 
  "Onerrorupdate" 
  "Onhelp" 
  "Onreadystatechange" 
  "Onrowenter" 
  "Onrowexit" 
  "Onselectstart" 
  "Onload" 
  "Onunload" 
  "Onbeforeunload" 
  "Onblur" 
  "Onerror" 
  "Onfocus" 
  "Onresize" 
  "Onscroll" 
  "Oncontextmenu" 
  ); 

  Content = replace (content, "<script", "& ltscript" false); 

content = replace(content, “</script”, “&lt/script”, false);

content = replace(content, “   Content = replace (content, "    Content = replace (content, "\ r \ n", " 
");
  / / Filtering script code incident 
  For (int i = 0; i <eventKeywords.length; i + +) ( 
  Content = replace (content, eventKeywords [i], "_" + eventKeywords [i], false); / / add a "_" so that the incident invalid code 
  ) 

  Return content; 
  ) 

  //******************************** Filter HTML code for the text of the code ********* ******************** 
  Public String replaceHtmlToText (String input) ( 
  If (isEmpty (input)) ( 
  Return ""; 
  ) 
  Return setBr (setTag (input)); 
  ) 

  / / Filter HTML tags 
  Public String setTag (String s) ( 
  Int j = s.length (); 

  StringBuffer stringbuffer = new StringBuffer (j + 500); 

  For (int i = 0; i <j; i + +) 
  If (s.charAt (i) == '<') 
  Stringbuffer.append ("<"); 
  Else if (s.charAt (i) == '>') 
  Stringbuffer.append (">"); 
  Else if (s.charAt (i) == '&') 
  Stringbuffer.append ("&"); 
  Else 
  Stringbuffer.append (s.charAt (i)); 

  Return stringbuffer.toString (); 
  ) 

  / / BR code filtering 

  Public String setBr (String s) ( 
  Int j = s.length (); 

  StringBuffer stringbuffer = new StringBuffer (j + 500); 

  For (int i = 0; i <j; i + +) 
  If (s.charAt (i) == '\ n') 
  Stringbuffer.append (""); 
  Else if (s.charAt (i) == '\ r') 
  Stringbuffer.append (""); 
  Else 
  Stringbuffer.append (s.charAt (i)); 

  Return stringbuffer.toString (); 
  ) 

  / / Filter box 
  Public String setNbsp (String s) ( 
  Int j = s.length (); 

  StringBuffer stringbuffer = new StringBuffer (j + 500); 

  For (int i = 0; i <j; i + +) 
  If (s.charAt (i) == '') 
  Stringbuffer.append ( ""); 
  Else 
  Stringbuffer.append (s.charAt (i)); 

  Return stringbuffer.toString (); 
  ) 

  / / String determine whether all characters 
  Public boolean isNumeric (String input) ( 
  If (isEmpty (input)) 
  Return false; 
  For (int i = 0; i <input.length (); i + +) ( 
  Char charAt = input.charAt (i); 
  If (charAt <'0 '| | charAt>'9') ( 
  Return false; 
  ) 
  ) 
  Return true; 
  ) 

  / / Read the conversion of data from a form of code 
  Public String toChi (String input) ( 
  Try ( 
  Byte [] bytes = input.getBytes ( "ISO8859-1"); 
  Return new String (bytes); 
  ) 
  Catch (Exception ex) ( 
  ) 
  Return null; 
  ) 

  / / Single 'changed''; SQL rules: If single quotes in a string containing embedded quotes, you can use two single quotes that embedded single quotes. 
  Public String replaceSql (String input) ( 
  Return replace (input, " '" "''"); 
  ) 

  / / Characters of a given URL encoding 
  Public String encode (String value) ( 
  If (isEmpty (value)) 
  Return ""; 
  Return java.net.URLEncoder.encode (value); 
  ) 

  / / Characters of a given URL decoder 
  Public String decode (String value) ( 
  If (isEmpty (value)) 
  Return ""; 
  Return java.net.URLDecoder.decode (value); 
  ) 

  / ** 
  * Judgement not empty string 
  * / 
  Public boolean isEmpty (String input) ( 
  If (input == null | | input.length () <= 0) 
  Return true; 
  Return false; 
  ) 

  / / Will be amended as mood symbols corresponding picture ————- please amend the relevant code pages! 
  Public String smilies (String temp) ( 
  If (isEmpty (temp)) 
  Return ""; 
  / / Determine whether there is any prohibited form of expression characters 
  / / If (isEmpty (request ( "smilies"))) ( 
  Temp = replace (temp, "/:)", 
"");
  Temp = replace (temp, "/: d" 
"");
  Temp = replace (temp, "/: o", ""); 
  Temp = replace (temp, "/: p" 
"");
  Temp = replace (temp, "/;)", ""); 
  Temp = replace (temp, "/:(", ""); 
  Temp = replace (temp, "/: s" 
"");
  Temp = replace (temp, "/:|", 
"");
  Temp = replace (temp, "/:$", 
"");
  / /) 
  Return temp; 
  ) 

  / ** 
  * Get the document extension. 
  * @ Param fileName need to be addressed in the name. 
  * @ Return the extension portion of the file's name. 
  * / 
  Public String getExtension (String fileName) ( 
  If (fileName! = Null) ( 
  Int i = fileName.lastIndexOf ('.'); 
  If (i> 0 & & i <fileName.length () - 1) ( 
  Return fileName.substring (i + 1). ToLowerCase (); 
  ) 
  ) 
  Return ""; 
  ) 

  / ** 
  * Get the prefix of the document. 
  * @ Param fileName need to be addressed in the name. 
  * @ Return the prefix portion of the file's name. 
  * / 
  Public String getPrefix (String fileName) ( 
  If (fileName! = Null) ( 
  Int i = fileName.lastIndexOf ('.'); 
  If (i> 0 & & i <fileName.length () - 1) ( 
  Return fileName.substring (0, i); 
  ) 
  ) 
  Return ""; 
  ) 

  / / JSP parameter approach ———————————— ——– ————————————– 
  / / ASP with a similar approach, returning to the form field value, and code conversion - applies only to Tomcat 4.0 
  Public String request (HttpServletRequest request, String fieldName) ( 
  / / POST method without parameters coding errors 
  If (request.getMethod (). EqualsIgnoreCase ( "POST")) ( 
  / / File upload mode 
  / / If (isUploadMode) ( 
  / / Return request.getParameter (fieldName); 
  / /) 
  / / For Tomcat 4.0.6 
  Return toChi (request.getParameter (fieldName)); 
  ) 
  Else ( 
  / / GET will be sent through the decoding Chinese characters (but must use java.net.URLEncoder parameters of a Chinese character coding) 
  / / Decoder, and requires the use of code conversion, anti-coding can be used, namely: return decode (request.getParameter (fieldName)); 
  / /: Decode () only applies to JDK 1.3 + Tomcat 4.0 
  Return toChi (request.getParameter (fieldName)); 
  ) 
  ) 

  / / If the value is null form, return "," avoid NullPointerException 
  Public String request1 (HttpServletRequest request, String fieldName) ( 
  String s = request (request, fieldName); 
  If (s == null) 
  Return ""; 
  Else 
  Return s; 
  ) 

  / / Designated form field value, and single 'changed''; SQL rules: If single quotes in a string containing embedded quotes, 
  / / Can be said that the use of two single quotes embedded single quotes. 
  Public String requestSql (HttpServletRequest request, String fieldName) ( 
  Return replaceSql (request1 (request, fieldName)); 
  ) 

  / / In accordance with the request by the name Cookie Cookie in the value prior to the need to request an initial value _ if Cookie value is null, then return to the "" 
  Public String getCookieValue (HttpServletRequest request, String name) ( 
  Cookie [] = cookies request.getCookies (); 
  If (cookies == null) 
  Return ""; 
  For (int i = 0; i <cookies.length; i + +) ( 
  Cookie cookie cookies = [i]; 
  If (cookie.getName (). Equals (name)) ( 
  / / Cookie need for the Chinese characters used in a URL-coding, application version: Tomcat 4.0 
  Return decode (cookie.getValue ()); 
  / / Do not require anti-coding, application Version: 1.0.1 JSWDK 
  / / Return cookie.getValue (); 
  ) 
  ) 
  / / A cookie may not return a null value, and may return a "" 
  Return ""; 
  ) 

  / / Designated form of an array 
  Public String [] getParameterValues (HttpServletRequest request, String name) ( 
  / / POST method without parameters coding errors 
  If (request.getMethod (). EqualsIgnoreCase ( "POST")) ( 
  / / File upload mode 
  / / If (isUploadMode) ( 
  / / Return request.getParameterValues (name); 
  / /) 
  / / - For Tomcat 4.0 
  Return request.getParameterValues (name); 
  / / - For JSWDK 1.0.1 
  / * String values [] = _request.getParameterValues (name); 
  If (values! = Null) ( 
  For (int i = 0; i <values.length; i + +) ( 
  Values [i] = toChi (values [i]); 
  ) 
  ) 
  Return values * / 
  ) 
  Else ( 
  / / GET will be sent through the decoding Chinese characters (but must use java.net.URLEncoder parameters of a Chinese character coding) 
  / / Decoder, and requires the use of code conversion, anti-coding can be used, namely: return decode (_request.getParameter (name)); 
  / /: Decode () only applies to JDK 1.3 + Tomcat 4.0 
  String values [] = request.getParameterValues (name); 
  If (values! = Null) ( 
  For (int i = 0; i <values.length; i + +) ( 
  Values [i] = toChi (values [i]); 
  ) 
  ) 
  Return values; 
  ) 
  ) 

  / / Delete the specified directory of Web applications under upload documents 
  Public void deleteFile (ServletContext application, String filePath) ( 
  If (! IsEmpty (filePath)) ( 
  String physicalFilePath = application.getRealPath (filePath); 
  If (! IsEmpty (physicalFilePath)) ( 
  Java.io.File file = new java.io.File (physicalFilePath); 
  File.delete (); 
  ) 
  ) 
  ) 

  ) 

  </ Td> </ tr> <tr> 

  ↑ Back 

String_getBytes () method in the Chinese coding problems

  Abstract: String_getBytes () method in the Chinese coding problems 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="684" height="20"> 

  String of getBytes () method is given a string array of bytes, which is well known.    However, with particular attention to is that this method will return to the operating system default encoding format of the byte array.    If you use this method does not take this into account, you will find a good platform to run on the system, put it in another machine will produce unexpected problems.    For example, the following procedures: <table width="675" border="0"> <tr> <td width="394"> 

  1.   Class TestCharset 
  2.   ( 
  3.   Public static void main (String [] args) 
  4.   ( 
  5.   New TestCharset (). Execute (); 
  6.   ) 
  7.   Private void execute () ( 
  8.   String s = "Hello! Hello!"; 
  9.   Byte [] bytes = s.getBytes (); 
  10.   System. Out.println ( "bytes lenght is:" + bytes. Length); 
  11.   ) 
  12.   ) 
  </ Td> <td width="271"> </ td> </ tr> </ table> 

  In a Chinese WindowsXP system, the run-time, results were as follows: 
  Bytes lenght is: 12 

  But if English has been put on a UNIX operating environment: 
  $ Java TestCharset 
  Bytes lenght is: 9 

  If you rely on the results of procedures, in the follow-up operation will cause problems.    Why, in a system in the results for 12, and in addition has turned out to a 9?    Already mentioned, the method is and platforms (code) related.    In the Chinese operating system, getBytes method is a return of the Chinese GBK or GB2312 encoded byte array, including Chinese characters, each with two bytes.    In English platform, the general default coding is "ISO-8859-1", each byte characters only take one (regardless of whether the non-Latin characters). 

  Java support in the coding 


  Java is the support for multi-country code, in Java, Unicode characters are stored, for example, "you" Unicode character encoding is "f60", we can test the adoption of the following code to verify: 
  1.   Class TestCharset 
  2.   ( 
  3.   Public static void main (String [] args) 
  4.   ( 
  5.   Char c = 'you'; 
  6.   Int i = c; 
  7.   System. Out.println (c); 
  8.   System. Out.println (i); 
  9.   ) 
  10.   ) 

  Whether you on all platforms, will have the same output: 

  —————— —————– Output 
  You 
20320

  20,320 is Unicode "4f60" integer values.    In fact, you can decompile the above categories can be found in the generated. Class characters in the document "you" (or any other Chinese string) itself is stored in Unicode encoding: 
  1.   Char c = '\ u4F60'; 
  2.   … … 

  Even if you know the code encoding formats, such as: 
  Javac-encoding GBK TestCharset.java 
  After the compiler generated. Class document storage format is still Unicode Chinese characters or strings. 

  Use String.getBytes (String charset) method 


  Therefore, in order to avoid such problems, I suggest that you use in coding String.getBytes (String charset) method.    Below we were extracted from the string ISO-8859-1 encoding formats and GBK two byte array and see what the outcome will be: 
  1.   Class TestCharset 
  2.   ( 
  3.   Public static void main (String [] args) 
  4.   ( 
  5.   New TestCharset (). Execute (); 
  6.   ) 
  7.   Private void execute () ( 
  8.   String s = "Hello! Hello!"; 
  9.   BytesISO8859 byte [] = null; 
  10.   BytesGBK byte [] = null; 
  11.   Try 
  12.   ( 
  13.   BytesISO8859 = s.getBytes ( "iso-8859-1"); 
  14.   BytesGBK = s.getBytes ( "GBK"); 
  15.   ) 
  16.   Catch (java.io. UnsupportedEncodingException e) 
  17.   ( 
  18.   E.printStackTrace (); 
  19.   ) 
  20.   System. Out.println ("————– \ n 8859 bytes: "); 
  21.   System. Out.println ( "bytes is:" + arrayToString (bytesISO8859)); 
  22.   System. Out.println ( "hex format is:" + encodeHex (bytesISO8859)); 
  23.   System. Out.println (); 
  24.   System. Out.println ("————– \ n GBK bytes: "); 
  25.   System. Out.println ( "bytes is:" + arrayToString (bytesGBK)); 
  26.   System. Out.println ( "hex format is:" + encodeHex (bytesGBK)); 
  27.   ) 
  28.   Public static final String encodeHex (byte [] bytes) ( 
  29.   StringBuffer buff = new StringBuffer (bytes. length * 2); 
  30.   String b; 
  31.   For (int i = 0; i    Length; i + +) 
  32.   ( 
  33.   B = Integer. ToHexString (bytes [i]); 
  34.   / / Byte is two bytes, and the above Integer.toHexString will be extended to four bytes bytes 
  35.   Buff.append (b. length ()> 2? B.substring (6,8): b); 
  36.   Buff.append ( ""); 
  37.   ) 
  38.   Return buff.toString (); 
  39.   ) 
  40.   Public static final String arrayToString (byte [] bytes) ( 
  41.   StringBuffer buff = new StringBuffer (); 
  42.   For (int i = 0; i    Length; i + +) 
  43.   ( 
  44.   Buff.append (bytes [i] + ""); 
  45.   ) 
  46.   Return buff.toString (); 
  47.   ) 
  48.   ) 
  Implementation of this program will print out: 
  1. ————–
  2.   8859 bytes: 
  3.   Bytes is: 7210110810811133 63 63 63 
  4.   Hex format is: 48 65 6c 6c 6f 21 3f 3f 3f 
  5. ————–
  6.   GBK bytes: 
  7.   Bytes is: 7210110810811133 -60 -29 -70 -61 -93 -95 
  8.   Hex format is: 48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1 

  Thus, even in the extraction of 8859-1 s format for the length of the array of bytes 9, Chinese characters have become "63", ASCII code 63 is a "?", Some foreign procedures in the Chinese domestic operating environment , often garbled, full of the above "?", it is because the code does not correctly deal with the results.    And the extraction of GBK encoded byte array in the correct Chinese characters have been GBK coding.    Characters "you", "good" and "!" GBK code are: "c4e3" "bac3" "a3a1."    Have the right to GBK encoded byte array, reducing the need for future Chinese string, you can use the following method: 
  New String (byte [] bytes, String charset) 

  The article ↑ Back 

keep looking »