Cadena String
Cual es la respuesta de:
publicclass Cadenas {
public static void main (String [] args) {
// Bloque 1
String s = new String("Viernes");
if (s=="Viernes") System.out.println("Respuesta A");
if (s.equals("Viernes"))
System.out.println("Respuesta B");
// Bloque 2
int num1 = 1234567;
System.out.println(String.valueOf(num1).charAt(3));
// Bloque 3 String s1 = "Lunes";
String s2 = "Martes";
System.out.println(s1.concat(s2).substring(4,8));
// Bloque 4
// s3 inicia con 2 espacios y tambientermina con 2.
String s3 = " Lunes "; System.out.println(s3.indexOf("es"));
System.out.println(s3.trim().indexOf("es"));
}
}
2***
public static void main(String[] args) {
// TODO code application logic here//longitud del string
String str="El primer programa";
System.out.println("longitud "+str.length());
//comienza y termina por...
str="El primer programa";
System.out.println("comienza porEl "+str.startsWith("El"));
System.out.println("termina por programa "+str.endsWith("programa"));
//posición de un carácter
int pos=str.indexOf('p');
System.out.println("posición de la letrap "+pos);
//segunda ocuurencia de p
pos=str.indexOf('p', pos+1);
//comparación
str="Tomás";
System.out.println("Orden alfabético "+str.compareTo("Alberto"));
str="Alberto";System.out.println("Orden alfabético "+str.compareTo("Tomás"));
System.out.println("Orden alfabético "+str.compareTo("Alberto"));
//substring
str="El lenguaje Java";
System.out.println("Substring"+str.substring(12));
System.out.println("Substring "+str.substring(3, 11));
//eliminar los espacios en blanco
str=" 12 ";
System.out.println("string original "+str);System.out.println("string sin espacios en blanco "+str.trim());
//convertir un número en string
int valor=24;
str=String.valueOf(valor);
System.out.println("número --> string "+str);...
Regístrate para leer el documento completo.