package com.codemonkey;
import static java.lang.System.err;
import static java.lang.System.out;
import java.lang.StringBuffer;//No es necesaria la importacion
//StringBuffer es mutable (lo contrario a String)
//String no posee metodos append, reverse,insert ni delete
//StringBuffer es mejor en hilos (thread)
public class Main{
public static void main(String[] args){
stringBufferDemo();
}
public static void stringBufferDemo(){
StringBuffer sb = new StringBuffer();//16 de capacity
int capacidad = sb.capacity();
//se usa .toString() para convertirlo a String
out.printf("%s\n",sb.toString());//
out.printf("%d\n",sb.capacity());//16
sb = sb.append(new String("01234-789"));//"".append(01234-789)
out.printf("%s\n",sb.toString());//01234-789
out.printf("%d\n",capacidad);//16
sb.append(new String("0"));//01234-789.append(0)
out.printf("%s\n",sb.toString());//01234-7890
sb = sb.append(new String("0"));//sb = 01234-7890.append(0)
out.printf("%s\n",sb.toString());//01234-78900
//01234-78900 ; indice 4 [0,1,2,3,|4|,5,6,7,8,9,10]
//0123|4|-78900
char caracter4 = sb.charAt(4);//indice 4 [0,1,2,3,4,5,6,7,8,9,10]
out.println(caracter4);//'4'
//01234-78900 ; indice 5 [0,1,2,3,4,|5|,6,7,8,9,10]
//01234|-|78900
int caracterUnicodeEn = sb.codePointAt(5);//-
out.println("- vale:"+caracterUnicodeEn);//45
//01234-78900 ; indice 7 [0,1,2,3,4,5,6,|7|,8,9,10]
//01234-7|8|900
int caracterUnicodeAntesDe = sb.codePointBefore(7);
out.println("7 vale:"+caracterUnicodeAntesDe);//55
//01234-78900 ; indice (2,5) [0,1,2,3,4,5,6,7,8,9,10]
//01|2|34|-|78900
int caracterEn = sb.codePointCount(2, 5);
out.println("2 : -"+caracterEn);
//01234-78900 ; indice (5,9) [0,1,2,3,4,5,6,7,8,9,10]
//01234|-|789|0|0
//el borrado inicia en 5 y finaliza en 8, 9 es el de no borrado
StringBuffer sbDelete = sb.delete(5, 9);
out.println(sbDelete.toString());//0123400
out.println(sb.toString());//0123400; StringBuffer es mutable
//0123400 ; [0,1,2,3,4,5,6]
StringBuffer borrarCaracterEn = sbDelete.deleteCharAt(5);
out.println(borrarCaracterEn.toString());//012340
borrarCaracterEn.deleteCharAt(5);
out.println(borrarCaracterEn.toString());//01234
out.println(sbDelete.toString());//01234
out.println(sb.toString());//01234
//StringBuffer es mutable
StringBuffer sbCapacidad = new StringBuffer();
// la capacidad de un StringBuffer es 16 por default
sbCapacidad.ensureCapacity(20);
out.println(sbCapacidad.capacity());//34
//01234 [0,1,2,3,4]
int indiceDe = sb.indexOf("3");
out.printf("%d\n",indiceDe);//3
//01234 [0,1,2,|3|,4]
//012A34 ; A en indice 3
StringBuffer sbInsert = sb.insert(3,"A");
out.println(sbInsert.toString());//012A34
out.println(sb.toString());//012A34
//012|A|34 [0,1,2,|3|,4,5]
int ultimoIndiceDe = sb.lastIndexOf("A");
out.printf("%d\n",ultimoIndiceDe);//3
//012A3|4| [0,1,2,3,4,|5|]
ultimoIndiceDe = sb.lastIndexOf("4");
out.printf("%d\n",ultimoIndiceDe);//5
int tam = sb.length();//012A34 , tam:6 [0,1,2,3,4,5]
out.println(sb.toString()+" tam: "+tam);//6
//012|A|3|4| inicia en 3(A) y fializa en 4(3)
sb = sb.replace(3, 4, "-");
out.println(sb.toString());//012-34
//012|-|34 [0,1,2,*3,4,*5]
// se pone 3 en indice 3 al 4 , el 5 es el de no reemplazo
sb.replace(3, 5, "3");
out.println(sb.toString());//01234
sb.replace(0,1,"BETA");
out.println(sb.toString());//BETA1234 [0,1,2,3,*4,5,7, *8]
//12345678
sb.replace(4,8,"MAX");
out.println(sb.toString());//BETAMAX
sb.reverse();
out.println(sb.toString());//XAMATEB
sb.reverse();
out.println(sb.toString());//BETAMAX
sb.setCharAt(0, new Character('M'));
out.println(sb.toString());//METAMAX
//METAMAX [0,1,2,3,4,5,6]
sb.setCharAt(4, new Character('L'));//METALAX
sb.setCharAt(5, new Character('M'));//METALMX
sb.setCharAt(6, new Character('A'));//METALMA
out.println(sb.toString());//METALMA
CharSequence charSequence = "54321";
StringBuffer sbLength = new StringBuffer(charSequence);
out.println(sbLength.toString());//54321
out.println(sbLength.capacity());//21 ; 16 + 5
out.println(sbLength.length());//5
sbLength.setLength(6);
out.println(sbLength.length());//6
sbLength.setLength(7);
out.println(sbLength.length());//7
sbLength.setLength(5);
out.println(sbLength.length());//5
out.println(sbLength.toString());//54321
sbLength.reverse(); //12345
out.println(sbLength.toString());
CharSequence subSecuencia = sbLength.subSequence(0, 2);
//12345 0>>1 2 3>>no copia : 12
out.println(subSecuencia );//12345 [0,1,2,3,4] >> 12
out.println(sbLength.toString());
//12345 >>34
subSecuencia = sbLength.subSequence(2,4);
out.println(subSecuencia);//34
//12345 [0,1,2,3,4]
String subCad = sbLength.substring(2);
out.printf("%s\n",subCad);//345
StringBuffer sbTrim = new StringBuffer(" Genesis ");
out.printf("%s\n",sbTrim.toString());
out.printf("%d\n",sbTrim.capacity());
sbTrim.trimToSize();
out.printf("%s\n",sbTrim.toString());
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.