package com.codemonkey;
import static java.lang.System.err;
import static java.lang.System.out;
import java.lang.StringBuilder;//No es necesaria la importacion
//StringBuilder es mutable
//String es inmutable y no tiene metodos append, insert, reverse ni delete
public class Main{
public static void main(String[] args){
stringBuilderDemo();
}
public static void stringBuilderDemo(){
StringBuilder sbd = new StringBuilder();//capacidad = 16
out.println(sbd.capacity());//16
out.println(sbd.length());//0
CharSequence charSequence = "01234";
sbd = new StringBuilder(charSequence);
out.println(sbd.capacity());//16 + 5 = 21
out.println(sbd.toString());//01234
sbd.append("56");
out.println(sbd.toString());//0123456
charSequence = "ABCD";
// 0123456.append(ABCD, 0, 2)
// 0123546.AB ; ABCD [*0,1,*2,3]
StringBuilder sbdA = sbd.append(charSequence, 0, 2);
out.println(sbd.toString());//0123546AB
out.println(sbdA.toString());//0123546AB
StringBuilder sbdB = sbdA.appendCodePoint(6);
out.println(sbd.toString());//0123456AB♠
out.println(sbdA.toString());//0123456AB♠
out.println(sbdB.toString());//0123456AB♠
out.println(sbd.capacity());//21
//0123456AB♠ [0,12,3,4,5,6,7,8,*9]
char caracterA = sbdB.charAt(9);
out.println(caracterA);//♠
//0123456AB♠
int codigoEn = sbdB.codePointAt(6);
out.println(codigoEn);//54
codigoEn = sbdB.codePointAt(9);
out.println(codigoEn);//6
codigoEn = sbdB.codePointAt(5);
out.println(codigoEn);//53
//0123456AB♠
int codigoAntesDe =sbdA.codePointBefore(9);
out.println(codigoAntesDe);//66
//0123456AB♠
int codigoCont = sbdA.codePointCount(0, 9);
out.println(codigoCont);//9
codigoCont = sbdA.codePointCount(3, 5);
out.println(codigoCont);//2
//0123456AB♠ [0,1,2,3,4,*5,6,*7,8,9]
// 0 1 2 3 4 A B ♠
//01234AB♠ *5 y 6 se elimina, *7 ya no se elimina
out.println(sbd.toString());
sbd.delete(5, 7);
out.println(sbd.toString());//01234AB♠
out.println(sbdA.toString());//01234AB♠
out.println(sbdB.toString());//01234AB♠
//01234AB♠ [0,12,3,4,5,6,7]
sbdA.deleteCharAt(new Integer(6));
out.println(sbd.toString());//01234A♠
out.println(sbdA.toString());//01234A♠
out.println(sbdB.toString());//01234A♠
//01234A♠
sbdB.deleteCharAt(5);
out.println(sbd.toString());//01234♠
out.println(sbdA.toString());//01234♠
out.println(sbdB.toString());//01234♠
out.println(sbd.capacity());//21
sbd.ensureCapacity(sbd.capacity()-6);
out.println(sbd.capacity());//21
char[] letras={'A'};
sbd.getChars(0, 0, letras, 0);
out.println(sbd.toString());//01234♠
int indiceDe = sbd.indexOf("4");
out.println(indiceDe);//4
out.println(sbd.toString());//01234♠
//01234♠ >> //X01234♠
sbdB.insert(0,"X");
out.println(sbd.toString());//X01234♠
//X01234♠ >> XT01234♠
sbdB.insert(1,"T");
out.println(sbd.toString());//XT01234♠
out.println(sbd.length());//8
//XT01234♠ [0,*1,2,3,*4,5,6,7]
// X *T 0 1 *2 3 4 ♠
int retval = sbd.offsetByCodePoints(1, 4);
out.println("indice = " + retval);//5
//XT01234♠ [0,1,*2,3,4,5,*6,7]
// X T *0 1 2 3 *4 ♠
retval = sbd.offsetByCodePoints(2, 6);
out.println("indice = " + retval);//8
//XT01234♠ [*0,1,2,*3,4,5,6,7]
// *X T 0 *1 2 3 4 ♠
// Alpha1234♠
sbd.replace(0, 3, "Alpha");
out.println(sbd.toString());//Alpha1234♠
sbdA.reverse();
out.println(sbd.toString());//♠4321ahplA
sbdB.reverse();
out.println(sbd.toString());//Alpha1234♠
//Alpha1234♠ [0,1,2,3,4,5,6,7,8,*9]
// A l p h a 1 2 3 4 ♠
char michar = '\uFF00';
sbd.setCharAt(9,michar);
out.println(sbd.toString());//Alpha1234?
//Alpha1234? [0,1,2,3,4,5,6,7,*8,9]
// A l p h a 1 2 3 *4 ?
michar = '\u0FF0';
sbd.setCharAt(8,michar);
out.println(sbd.toString());//Alpha123??
sbdB.setLength(23);
out.println(sbd.length());//23
out.println(sbdA.length());//23
out.println(sbdB.length());//23
//Alpha1234? [0,1,2,3,4,5,6,7,*8,*9]
// A l p h a 1 2 3 *? *?
CharSequence secuencia = sbdA.subSequence(8, 9);
out.println(sbdB.toString());//Alpha123??
out.println(secuencia);//?
//Alpha1234? [0,1,2,3,4,5,6,7,*8,*9]
// A l p h a 1 2 *3 ? *?
String subStr = sbd.substring(7,9);
out.println(subStr);//3?
sbd.trimToSize();
out.println(sbdB.toString());//Alpha123??
}
}
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.