package com.codemonkey;
import static java.lang.System.out;
import java.lang.String;//No es necesaria la importación
//String es inmutable
public class TestString{
public static void main(String[] args){
stringDemo();
}
public static void stringDemo(){
String cadena = new String("Adelaida");//[A,d,e,l,a,i,d,a] [0,1,2,3,4,5,6,7]; tam:8
char caracterA = cadena.charAt(0);//'A'
int codigoEn = cadena.codePointAt(0);//65
int codigoAntes = cadena.codePointBefore(7);//100
int codigoAntesFinal = cadena.codePointCount(0, 7);//7
int comparadoX = cadena.compareTo("Adelaida");//0
int comparadoY = cadena.compareTo(new String("Adelaida"));//0
int comparadoZ = cadena.compareTo(new String("Adela"));//3
int comparadoC= cadena.compareToIgnoreCase("Adelaida");//0
String miCadena = cadena.concat(" "
+ new String("la gata mas fabulosa"));//Adelaida la gata mas fabulosa
CharSequence charSequence = "fabulosa";
boolean tieneFabulosa= miCadena.contains(charSequence);//true
boolean tieneLaGata = miCadena.contentEquals("la gata");//false
boolean tieneAdelaida = miCadena.contentEquals(new StringBuffer("Adelaida"));//false
char[] letras = {'A','B','C','D'};
String copiarValores = String.copyValueOf(letras);//ABCD; copia todo
String copiarValoresDe = String.copyValueOf(letras,0,2);//AB; copia de 0 a 1, el 2 es el de no copia
String docPDF = new String("documento.pdf");
boolean esPDF = docPDF.endsWith(".pdf");//true, documento.pdf termina en .pdf
boolean esIgual = cadena.equals(new String("Adelaida"));//true
boolean esCasiIgual = cadena.equalsIgnoreCase(new String("adelaida"));//true
String formateada = String.format("%s = %d", "Fer", 35);
byte[] misBytes = "ABC".getBytes();//[B@21a06946
int indiceX = cadena.indexOf(0);
int indiceZ = cadena.indexOf(cadena.length());
int indiceY = cadena.indexOf(3);
int indiceA = cadena.indexOf("A");
int indiceD = cadena.indexOf("d");
int indiceE = cadena.indexOf("e");
int tamanyo = cadena.length();//8 [Adelaida]
String interno = cadena.intern();
boolean vacio = cadena.isEmpty();
String unir = String.join("-","Bienvenido","a","mi laberinto");//Bienvenido-a-mi laberinto
int ultimoIndex = cadena.lastIndexOf("A");//0
int ultimoIndexDe = "Roma".lastIndexOf("o");//1
boolean hayComa= "Si, el la mato".matches(",");//false
int codigos = cadena.offsetByCodePoints(0,7);
String Str1 = new String("Mi laberinto de los naufragios");
String Str2 = new String("laberinto");
String Str3 = new String("Naufragios");
String strNombre = "Fernando".replace('o', 'a');
String strOracion = "El incipiente mago de la cueva encantada".replaceAll("a","e");
String strParrafo = "La naranja mecanica".replaceFirst("a","o");//Lo naranja mecanica
String[] cadenas = "12,43,55,90".split(",");
String strBiblio = new String("GENESIS - APOCALYPSIS");
String subCadena = "01234-789".substring(6,9);//789
char[] charStr = "abecedario".toCharArray();
String strMinuscula = "ANITA LAVO LATINA".toLowerCase();
String strMayuscula = "el murcielago de valladolid".toUpperCase();
String strNumero = "123".toString();
String strEspacio = " La genetica del gato ".trim();
String strValor = String.valueOf(new Double("34.0"));
out.println("cadena) = "+cadena);
out.println("charAt(0) = "+caracterA);
out.println("codePointAt(0) = "+codigoEn);
out.println("codePointBefore(7) = "+codigoAntes);
out.println("codePointCount(0, 7) = "+codigoAntesFinal);
out.println("compareTo(\"Adelaida\") = "+comparadoX);
out.println("compareTo(\"new String(Adelaida)\") = "+comparadoY);
out.println("compareTo(\"new String(Adela)\") = "+comparadoZ);
out.println("compareToIgnoreCase(\"Adelaida\") = "+comparadoC);
out.printf("%s\n",miCadena);
out.println(tieneFabulosa? "Si, tiene \"fabulosa\"" : "No, no tiene \"fabulosa\"");//true
out.println(tieneLaGata? "Si, tiene \"la gata\"" : "No, no tiene \"la gata\"");//false
out.println(tieneAdelaida? "Si, tiene \"Adelaida\"" : "No, no tiene \"Adelaida\"");//false
out.printf("%s\n",copiarValores);//ABCD
out.printf("%s\n",copiarValoresDe);//AB
out.println(esPDF? "Si, es pdf" : "No, no es pdf");//Si, es pdf
out.println(esIgual? "Si, es igual" : "No, no es igual");//Si, es igual
out.println(esCasiIgual? "Si, es casi igual" : "No, no es casi igual");//Si, es casi igual
out.printf("%s\n",formateada);//Fer = 35
out.println(misBytes);//[B@21a06946
out.printf("%s.indexOf(0) = %d \n",cadena,indiceX);//-1
out.printf("%s.indexOf(8) = %d \n",cadena,indiceZ);//-1
out.printf("%s.indexOf(3) = %d \n",cadena,indiceY);//-1
out.printf("%s.indexOf(A) = %d \n",cadena,indiceA);//0
out.printf("%s.indexOf(d) = %d \n",cadena,indiceD);//1
out.printf("%s.indexOf(e) = %d \n",cadena,indiceE);//2
out.printf("%s.length()= %d \n",cadena,tamanyo);//8
out.printf("%s\n",interno);//Adelaida
out.println(vacio? "Cadena vacia" : "Cadena llena");//Cadena llena
out.printf("%s\n",unir);//Bienvenido-a-mi laberinto
out.printf("%s\n",ultimoIndex);//0
out.printf("%s\n",ultimoIndexDe);//1
out.println(hayComa? "Si, hay coma" : "No, no hay coma");//No, no hay coma
out.println("tam cadena = "+codigos);//7
out.println(" :"+Str1.regionMatches(11, Str2, 0, 9));//false
out.println(" :" +Str1.regionMatches(11, Str3, 0, 9));//false
out.println(strNombre);//Fernanda
out.println(strOracion);//El incipiente mego de le cueve encentede
out.println(strParrafo);//Lo naranja mecanica
recorrerStr(cadenas);// 12 43 55 90
if("documento.xml".startsWith("doc")){
out.println("El archivo es un documento");//Inicia con doc
}
out.println(strBiblio.subSequence(0,4));//GENE
out.println(strBiblio.subSequence(10,16));//APOCAL
out.printf("%s\n",subCadena);//789
out.println(charStr);//abecedario
out.printf("%s\n",strMinuscula);
out.printf("%s\n",strMayuscula);
out.printf("%s\n",strNumero);
out.printf("%s\n",strEspacio);
out.printf("%s\n",strValor);
}
public static void recorrerStr(String[] cads){
for(String cad : cads){
out.println(cad);
}
}
}
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.