String Usage Precautions

xiaoxiao2021-03-06  50

String comparison:

A string object is used in Java through an object.

Equals method: When comparing whether the contents of the two string objects are equal, the equals method should be used.

== Operator: == Comparison string object reference is equal.

String and other types of conversions:

Type Conversion String From String Conversion

Boolean string.valueof (boolean) new boolean (String) .booleanValue ()

Byte string.valueof (int) Byte.Parsebyte (String, Int Base)

Short string.valueof (int) short.parseshort (String, int base)

Int string.valueof (int) integer.parseshort (String, int base)

Long string.valueof (long) long.parselong (string, int base)

Float string.valueof (float) float.parselong (String, int base)

Double string.valueof (double) double.parselong (string, int base)

String and Char array conversion:

Create string from char: constructor public string (char [] Chars, int Start, int count)

Public String (char [] chars)

String.copyValueof ()

Convert from String to Char: Tochararray (), GetChars

StringBuffer use

Each string object is read-only. During the operation of strings, you may need to create multiple intermediate objects, which will experience the process of creation, assignment, and garbage collection.

You can create a StringBuffer object, add an element to it, and then create a String object from the resulting result from the resulting result.

Such as: public static string guillemete (string quote) {

Return << ' quote ' >> ';

}

The compiler will do this:

Quoted = String.Valueof ('<<'). Concat (quote) .concat (String.Valueof ('>>'));

StringBuffer can be used, indicating as follows:

Quote = new stringbuffer (). append ('<<'). append (quote) .append ('>>'). Tostring;

转载请注明原文地址:https://www.9cbs.com/read-116846.html

New Post(0)