Results 1 to 3 of 3
- 07-21-2011, 03:27 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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
- 07-21-2011, 03:39 AM #2
This code:
generates this output: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);
R nL=new_line.replace(.75, .75)
nL=new_line.replace(.75, .75)
nL=replaceAll((C), (C))
- 07-21-2011, 03:46 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 08:22 PM -
create string from array of chars
By leda in forum New To JavaReplies: 1Last Post: 11-08-2010, 06:39 PM -
Make String into chars
By myst in forum New To JavaReplies: 19Last Post: 06-20-2010, 04:24 PM -
Swap chars in a String?
By spatel14 in forum New To JavaReplies: 5Last Post: 06-08-2010, 09:05 PM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks