martes, 1 de marzo de 2016

Funcion Replace en Java

String Str = "This is a String to use as an example to present raplaceAll";

        // replace all occurrences of 'a' with '@'
        String newStr = Str.replaceAll("a", "@");
        System.out.println(newStr);

        // replace all occurrences of 'e' with '3'
        newStr = newStr.replaceAll("e", "3");
        System.out.println(newStr);

        // replace all occurrences of 't' with 'T'
        newStr = Str.replaceAll("t", "T");
        System.out.println(newStr);

        // remove all occurrences of 'o'
        newStr = Str.replaceAll("o", "");
        System.out.println(newStr);


        // replace all occurrences of 't' with 'That'
        newStr = Str.replaceAll("T", "That");
        System.out.println(newStr);

    This will output :
    This is @ String to use @s @n ex@mple to present r@pl@ceAll
    This is @ String to us3 @s @n 3x@mpl3 to pr3s3nt r@pl@c3All
    This is @ STring To us3 @s @n 3x@mpl3 To pr3s3nT r@pl@c3All
    This is @ STring T us3 @s @n 3x@mpl3 T pr3s3nT r@pl@c3All
    Thathis is @ SThatring That us3 @s @n 3x@mpl3 That pr3s3nThat     r@pl@c3All

       
    String str = "This 1231 is 124 a String 1243 to 34563 use 5455";

        // remove all numbers
        String newStr = Str.replaceAll("[0-9]+", "");
        System.out.println(newStr);

        // remove all words with 'Java'
        newStr = Str.replaceAll("[a-zA-Z]+", "Java");
        System.out.println(newStr);

        This will output :

        This  is  a String  to  use
        Java 1231 Java 124 Java Java 1243 Java 34563 Java 5455


Mis Otros Blogger
http://manualesimportantesdeempresarios.blogspot.com/
http://trabajarporinternetenvenezuela.blogspot.com/
http://lomejordelaprogramacionjavayotros.blogspot.com/
http://fundacionformandoconcienciayvalores.blogspot.com/

No hay comentarios:

Publicar un comentario