Results 1 to 3 of 3
  1. #1
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Problem replacing chars in String.replaceAll(), Chars such as (1/2) ½ and (1/4) ¼

    I'm having a problem using the replace function with the String.replaceAll();

    In the following code, it doesn't seem to be able to see or replace certain chars for trademark, registered, and a couple of other chars.

    // I get the line of data from a file and store it in line.

    String new_line = "";
    new_line=line;
    new_line = new_line.replaceAll("\\?\"", "\"");
    new_line = new_line.replaceAll("\"\"", "-in.");
    new_line = new_line.replaceAll("\'", "\\'");
    new_line = new_line.replaceAll("\"", "");

    // Everything works up till this point..
    // contains doesn't see them and replace doesn't replace it even if it did see it.

    if (new_line.contains("¾")){
    new_line = new_line.replace("¾", ".75");
    System.out.printf("Contains a ¾ (3/4)");
    // System.exit(0);
    }


    if (new_line.contains("¼")){
    new_line = new_line.replace("¼", ".25");
    System.out.printf("Contains a ¼ (1/4)");
    System.exit(0);
    }

    if (new_line.contains("½")){
    new_line = new_line.replace("½", ".50");
    System.out.printf("Contains a ½ (1/2)");
    // System.exit(0);
    }

    // I doesn't work for these either.

    new_line = new_line.replaceAll("©", "(C)");
    new_line = new_line.replaceAll("®", "(R)");
    new_line = new_line.replaceAll("™", "(TM)");
    new_line = new_line.replaceAll("É", "E");
    new_line = new_line.replaceAll("é", "e");

    // Not really a show stopper, but, I"m getting tired of doing search and replaces..

    // Any help greatly appreciated..

    Jeff - jwilke101@gmail.com

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default

    This code:
    Java Code:
          String new_line = "new_line.replace(¾, .75)";
          if (new_line.contains("¾")){
              new_line = new_line.replace("¾", ".75");
              System.out.println("R nL=" + new_line);
          }
          System.out.println("nL=" + new_line);
    
          new_line = "replaceAll(©, (C))";
          new_line = new_line.replaceAll("©", "(C)");
          System.out.println("nL=" + new_line);
    generates this output:

    R nL=new_line.replace(.75, .75)
    nL=new_line.replace(.75, .75)
    nL=replaceAll((C), (C))

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Works for me:

    Java Code:
    public class ReplaceEg {
    	public static void main(String[] args) {
    		String test = "©©-- Asd © ©";
    		
    		test = test.replaceAll("©", "(C)");
    		System.out.println("output=" + test);
    	}
    }
    
    
    output=(C)(C)-- Asd (C) (C)
    -----

    It might be a good idea to output new_line after each replaceAll() so you can see the effect of that method. If you are still stuck provide a SSCCE that illustrates the problem.

Similar Threads

  1. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  2. create string from array of chars
    By leda in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 06:39 PM
  3. Make String into chars
    By myst in forum New To Java
    Replies: 19
    Last Post: 06-20-2010, 04:24 PM
  4. Swap chars in a String?
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 06-08-2010, 09:05 PM
  5. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •