Java strings "Chaohuaxijie"
<!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportEmptyParas] -> <!–[ endif] -> string is used A lot of class, even if this is the case, there are many strings to our confusion, there are many we do not have a clear place. Common usage, we have already Lanshu, here shall not repeat it. We just want to find some peace inadvertently place, we might have used, but it's not sure. Summed up by them, as "Chaohuaxijie". <!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportLists] -> First, <!–[ endif] -> on the String object String objects on the issue, often in the debate on the bbs. Now the tendency is explained: String s = "abc"; this phrase is a quote s, which targets at "abc." As a result, the following statement: String s1 = "abc"; String s2 = "abc"; just had an object, namely: "abc." The String s = new String ( "abc"); the phrase s is an object. The following statements: String s1 = new String ( "abc"); String s2 = new String ( "abc"); producing two objects, they are s1 and s2. With the above explanation, we have not difficult to understand why our priority in the code before use an expression, because the former is a way to use it, whether you want to use the code number of the same quote, produced only an object. Then a number of ways even if the contents of the same object, they are different objects, to have their own memory overhead; This has greatly increased our memory overhead. Similarly, the code below, as well as their operating results can be explained. String s1 = "abc"; String s2 = "abc";
String s3 = new String ( "abc");
Code: System.out.println (s1 == s2); operating results: true code: System.out.println (s1 == "abc"); The result: true code: System.out.println (s1.equals ( s2)); operating results: trueSystem.out.println (s1 == s3); operating results: false code: System.out.println (s3.equals ( "abc")); The result: true <!–[ if! supportEmptyParas] -> <!–[ endif ]–><!–[ if! supportLists] -> Second, <!–[ endif] -> String and StringBuffer Since the distinction between statements: String s = "abc" in the s is a quote. Then, in the following statement: String s = "abc"; s = s + "d", it should have three targets: "abc", "d" and "abcd." S first point cited targets, "abc", after pointing, "and abacd." In this way we can see the difference between String and StringBuffer to. The following code: String s = ""; for (int i = 0; i <10; i + +) (
S = s + i;
) We will have more than 10 targets. And the following code:
StringBuffer sb = new StringBuffer ();
For (int i = 0; i <10; i + +) (sb.append (i);) produced only an object. Therefore, in the String object need to add time, the use of StringBuffer is obviously superior than the use of String, particularly in the combined cycle time. <!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportLists] -> Third, <!–[ endif] -> other basic types String and the conversion of the actual coding process, often to other basic types into a String type, as in the above we are used to examples: String s = ""; for (int i = 0; i <10; i + +) (
S = s + i;
), S = s + i; statements on the need to integer i data into strings, and then combined with the s. In fact, apart from s = s +1; the way of such a transformation, and we are not a common approach to the conversion, as follows: s = s + Integer.toString (1) After all right right into the above two methods by the amount of time spent on testing and found that the manner in which the latter transformation is much lower than the time spent in front of a transformation of the manner in which consumption of time. Therefore, we in the process of coding, may wish to use the back of a transforming way. <!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportLists] -> Fourth, <!–[ endif] -> Object class toString () Heavy toString () method is a public Object class, Java Object type is the father of all categories. Therefore, any other Java class can rewrite the method to his own use. For example, all the basic types of packaging, such as: Integer, Float, Double, have rewritten the toString () method, is the type of function into a String type. The toString () generally used to provide some rewriting of such basic information. As for a plural categories, as follows:
Public class Complex
(Private double real; private double image;)…… its toString () method can be written as:
Public String toString ()
(String ri = ""; if (this.image <0) (ri = ri +"-"+ Double.toString (Math.abs (this.image)) + "i";) else (ri = ri + "+" Double.toString (this.image) + "i";) return Double.toString (this.real) + ri;) above the toString () method just to express a complex mathematical formula, it is precisely in the Complex computing and want to complete the results. Again, for a staff categories: Employee. We most commonly used, most often want to see their names, as follows:
Public class Employee
(Private String firstName; private String secodeName;…… public String toString () (
This.secodeName return this.firstName +;
……)) So, in some categories, rewritten toString () method is very useful, and may often used as a display data may also be used in debugging statements. <!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportLists] -> Fifth, <!–[ endif] -> Escape characters Escape character of the problem is our problem in the course of regular programming will be problems encountered. HTML programming such as encountered in the ">" or "<", the browser will retain their character as the HTML, such as tags at the beginning and the end of 1999 to look at this there will be some mistakes. Also JDBC Programming in the sql statement, if there is a "" "or" ' "characters, such as quotation marks, will go wrong, they will sql statement as a sql statement at the beginning or end. Moreover, URL phrase such as "?" characters, expressions URL will cause confusion. etc. encountered above these circumstances, we have to find some special characters on them be replaced, and then come back exhausted after replacement, Escape character is a special character, but we have to find their own escape characters First relatively trouble, but error-prone, easy to escape characters Zhangguanlidai situation. light of this, the Jakarta Commons package provides us with a set of characters escape solutions, the use of this programme, we do not need to go and find their own escape characters need only call the API Jakarta Commons packets can be, as follows:
String s = "There is always <(less) or> (more) than you want.";
String s1 = StringEscapeUtils.escapeHtml (s);
System.out.println (s1);
String s2 = StringEscapeUtils.unescapeHtml (s1);
System.out.println (s2.equals (s)); System.out.println (s2); output:
There is always <(less) or> (more) than you want.
True
There is always <(less) or> (more) than you want.
Is not very simple, we only need to introduce the above code Jakarta Commons in the corresponding packet can be. <!–[ If! SupportEmptyParas] -> <!–[ endif ]–><!–[ if! SupportLists] -> 6, <!–[ endif] -> English Cazi on the issue of mixed Chinese and English sentence Cazi problem is that I have seen in the bbs a peer solution. Find it very interesting, although I have not used until now, but put it in a summary of the article, the string is a collection of the usage of the bar. We all know that Java is used Unicode encoding, all of the characters are occupies a word, the two bytes. Then we mixed in a sentence in English, how the extracted one of the Chinese words and English words? Below is a simple solution: String s = "I was in the ab people of the country";
Byte [] = bt s.getBytes ();
Int i = 0;
While (i <bt.length) (String ss; if (bt [i] <0) (ss = new String (bt, i, 2) i + = 2;) else (ss = new String (bt, i , 1) i + +;) System.out.println (ss); }<!–[ if! supportEmptyParas] -> <!–[ endif] -> output results: I was in the ab people






