jueves, 6 de mayo de 2021

Un ejercicio de prueba con cadenas de caracteres en lenguaje Java

Un exercici senzill:
public class Cadena{
public static void main(String args[]){
String nom;
nom = "Miquel";
System.out.print("El nom" + nom + "té" + nom.length() + "caràcters\n");
System.out.print("El nom" + nom + "té la lletra 'q' en la posició" + nom.indexOf("q") + "\n");
}
}


Un exercici una mica més complet:

public class ProvaString{
public static void main (String args[] ){

// declaració de tipus de cada variable
String nom;
String nombre;
String name;
int position_letter_o;

// assignació de valor
nom="Joan";
nombre="Juan";
name="John";
position_letter_o=nom.indexOf("o")+ 1;
System.out.println("The word "+nom+" has "+nom.length()+" characters \n");
System.out.println("The first character of the word "+nom+" is "+nom.charAt(0)+"\n");
System.out.println("The word "+nom+" has the letter 'o' in "+position_letter_o+"nd position \n");
System.out.println("The word "+nom+" is the same as "+nom.toUpperCase()+" \n");
System.out.println("The first name "+nom+" (catalan) is the same as "+nom.replace(nom,nombre)+" in spanish \n");
System.out.println("The first name "+nom+" (catalan) is the same as "+nom.replace(nom,name)+" in english \n");
}
}

No hay comentarios:

Publicar un comentario