Class String: Java - Lang.object Java - Lang.string

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Class String

java.lang.Object
java.lang.String
The String class represents character strings. All string literals in Java programs, such as
"abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers
support mutable strings. Because String objects are immutable they can be shared.
The class String includes methods for examining individual characters of the sequence, for
comparing strings, for searching strings, for extracting substrings, and for creating a copy
of a string with all characters translated to uppercase or to lowercase.
A String represents a string in the UTF-16 format in which supplementary characters are
represented by surrogate pairs. Index values refer to char code units, so a supplementary
character uses two positions in a String.
The String class provides methods for dealing with Unicode code points (i.e., characters),
in addition to those for dealing with Unicode code units (i.e., char values).
1. La clase String pertenece al paquete java.lang.
2. Existen diferentes formas para crear objetos String como las siguientes :
String str =abc;
char str[] = {a,b,c}; //arreglo de caracteres
String str = new String (abc) ; //objeto con valor inicial abc
3. El lenguaje java proporciona el operador + para concatenar (unir) objetos String.










EJEMPLO UTILIZANDO METODOS DE LA
CLASE STRING
Ejemplo.java
package ejemplo;
import javax.swing.JOptionPane;
public class Ejemplo {
String a,b,c,d,aa;
static int opcion;
public static void main(String[] args) {
cod ee=new cod();
do{
opcion=Integer.parseInt(JOptionPane.showInputDialog("Ingresa una opcion:\n1.Mayusculas y
minusculas\n2.Reemplazar\n3.Salir"));
switch(opcion){
case 1:{
ee.may_min();
}break;
case 2:{
ee.rem();
}
}
}while(opcion!=3);
}
}
cod.java
package ejemplo;
import javax.swing.JOptionPane;
public class cod extends Ejemplo{
public void may_min(){
a=JOptionPane.showInputDialog("Pasar una cadena de texto a mayusculas y minusculas\nIngresa una cadena
de texto");
b=a.toLowerCase();
String c=a.toUpperCase();
JOptionPane.showMessageDialog(null, "CADENA ORIGINAL: "+a+"\nCADENA A MINUSCULAS:
"+b+"\nCADENA A MAYUSCULAS: "+c);
}
public void rem(){
a=JOptionPane.showInputDialog("Reemplazar un valor en una cadena de texto por otro\nIngresa una cadena
de texto");
c=JOptionPane.showInputDialog("Ingresa el valor que quieres que sea cambiado en la cadena de texto");
b=JOptionPane.showInputDialog("Ingresa el valor que quieres poner en la cadena de texto");
aa=a.toLowerCase();
if(aa.contains(c)){
d=aa.replace(c, b);
JOptionPane.showMessageDialog(null, "CADENA ORIGINAL: "+a+"\nCADENA CAMBIADA: "+d);
}else{
JOptionPane.showMessageDialog(null, "La cadena de texto no contiene el caracter: "+c);
}
}
}

__________________________________
Fuentes de informacin:
http://docs.oracle.com/javase/7/docs/api/
http://javax-peru.blogspot.mx/2009/11/la-clase-string.html

You might also like