Sie sind auf Seite 1von 4

UNIVERSIDAD POLITECNICA SALESIANA PROGRAMACION II

TEMA: Mtodos de la clase String. NOMBRE: Christian Xavier Uyaguari.

METODOS DE LA CLASE STRING

Los Strings son una secuencia de caracteres, incluyendo letras (maysculas y minsculas), signos, espacios, caracteres especiales, etc. En palabras simples, los Strings sirven para guardar palabras, oraciones, etc. En java los Strings son objetos, tipo String
[1]

int length().

Sencillamente, retorna la cantidad de caracteres de un String.

String texto1 = "Hola"; int n = texto1.length(); // 4

char charAt(int index). Retorna el caracter que se encuentra en la posicin dada por index. String texto = "Hola Juanito!"; char c2 = texto.charAt(1); // H

boolean startsWith(String s). Devuelve true o false si el parametro s es igual al string designado

String texto = "Hola Juanito!"; boolean b1 = texto.startsWith(""); // true. boolean b3 = texto.startsWith("hola Juanito!") // false.

METODOS DE LA CLASE STRING

int indexOf(char c). Devuelve un entero que indica la posicin donde aparece por primera vez el caracter String texto = "Hola Juanito!"; int n = texto.indexOf(o); // 2

int indexOf(String s). Devuelve la posicion donde aparece por primera vez un determinado String.

String texto = "En la arboleda un arbolito"; int n = texto.indexOf("arbol"); // 6 int m = texto.indexOf("arbol"); // 1

boolean equals(String s). Retorna true cuando ambos String son exactamente iguales.

String texto1 = "casa"; String texto2 = "caza"; boolean b1 = texto1.equals(texto); // false

int compareTo(String s). Este metodo compara lexicogracamente dos String.

String texto = "radiografa"; int n = texto.compareTo("radio"); // 6 int m = texto.compareTo("radiologa"); // -5

String substring(int inicio, int n) Retorna una parte del String: desde la posicin inicio hasta la posicin n

String texto = "desenfreno";


String subTexto = texto.substring(5, 10); // "freno" int n = texto1.length(); // 4

String replaceAll(String aReemplazar , String reemplazo). Reemplaza todas las ocurrencias de a Reemplazar por reemplazo.

String t1 = "El auto volaba rapido"; String t2 = t1.replaceAll("auto", "avion")); // "El avion volaba rapido"

boolean startsWith(String s, int k). Devuelve true o false si el parametro s es igual al string designado pero desde una posicion determinada.

String texto = "Hola Juanito!"; boolean b1 = texto.startsWith("Hola", 1); // true boolean b3 = texto.startsWith("a", 7); // false

METODOS DE LA CLASE STRING

int indexOf(char c, int fromIndex) y int indexOf(String s, int fromIndex) comienza a buscar posicion fromIndex. desde la

String texto = "En la arboleda un arbolito"; int n = texto.indexOf("arbol",4 ); // 2

toLowerCase(), toUpperCase(): Convierte el String a minsculas o maysculas.

String str="Hola Mundo"; str=str.toUpperCase(); // str queda como "HOLA MUNDO"

public String trim() Remueve los caracteres blancos al final del string.

String str="Hola Mundo "; str=str.trim(); // str queda como "Hola Mundo"

BIBLIOGRAFIA [1] http://web.ing.puc.cl/~iic1103/IIC1103/Inicio_files/Strings.pdf [2]hugoinda.netai.net/Downloads/Semestre.../Static_Final_String.doc [3]http://ocw.upm.es/lenguajes-y-sistemas-informaticos /programacion-en-java-i/Contenidos/LecturaObligatoria/10algunasclasesestandardejavai.pdf]

Das könnte Ihnen auch gefallen