Cov txheej txheem:

Yuav ua li cas koj tshem ib txoj hlua builder?
Yuav ua li cas koj tshem ib txoj hlua builder?

Video: Yuav ua li cas koj tshem ib txoj hlua builder?

Video: Yuav ua li cas koj tshem ib txoj hlua builder?
Video: nkauj huab lauj . koj tsis yuav kuv 2024, Kaum ib hlis
Anonim

1) Clear StringBuilder los ntawm kev muab cov khoom tshiab

  1. pej xeem chav JavaStringBufferClearEmptyExample {
  2. public static void main( Txoj hlua args) {
  3. StringBuilder sbStr = null;
  4. for(int i = 1; i <= 5; i++){
  5. // meej cov ntsiab lus los ntawm iteration yav dhau los.
  6. sbStr = new StringBuilder ();
  7. sbStr. append(i);
  8. Qhov system. tawm. print(sbStr);

Tom qab ntawd, kuv yuav hloov StringBuilder rau txoj hlua li cas?

Txhawm rau hloov StringBuilder rau String tus nqi yooj yim hu rau toString() txoj hauv kev rau nws

  1. Instantiate chav StringBuilder.
  2. Ntxiv cov ntaub ntawv rau nws siv append() txoj kev.
  3. Hloov cov StringBuilder rau txoj hlua siv txoj kev toString().

Kuv yuav ua li cas thiaj paub yog tias kuv tus StringBuilder khoob? Txoj kev ntev ntawm cov StringBuilder lossis StringBuffer chav kawm rov qab qhov ntev ntawm cov kab ke ntawm cov cim nws tam sim no muaj. Raws li koj tuaj yeem pom hauv qhov piv txwv, mus kuaj yog tias tus StringBuilder yog khoob , tau qhov ntev ntawm lub StringBuilder khoom. Yog ntev yog 0, nws yog khoob , txwv tsis pub.

Tsis tas li ntawd, koj yuav tshem tawm ib yam dab tsi los ntawm txoj hlua hauv Java li cas?

Koj tuaj yeem siv txoj kev substring() ntawm java ua . lang. Txoj hlua chav rau tshem tawm thawj los yog tus cim kawg ntawm Txoj hlua hauv Java . Txoj kev substring() yog overloaded thiab muab ob peb versions uas tso cai rau koj mus tshem tawm tus cwj pwm los ntawm txhua txoj haujlwm hauv Java.

Qhov twg zoo dua StringBuffer lossis StringBuilder?

Txoj hlua yog immutable whereas StringBuffer thiab StringBuider yog cov chav kawm hloov tau. StringBuffer yog xov zoo thiab synchronized whereas StringBuilder tsis yog, yog vim li cas StringBuilder yog nrawm dua StringBuffer . Txoj hlua concat + tus neeg teb xov tooj siv sab hauv StringBuffer lossis StringBuilder chav kawm.

Pom zoo: