Results 1 to 4 of 4
Thread: Problem with replace word
- 12-11-2011, 05:28 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Problem with replace word
I am using replace word function,try to replace "\" to "/".
this is my code:
And it give me this error...Java Code:import java.lang.*; public class test { public static void main(String[] args) { String str="We want \\ replace word from \\ this string"; str=str.replaceAll("\\", "/"); System.out.println(str); } }
I wonder why?Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected in
ternal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at test.main(test.java:7)
Can anyone help me?
- 12-11-2011, 05:45 PM #2
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Problem with replace word
Your first argument is a regular expression.
Look at what needs to be passed in for a backslash..
source: Pattern (Java 2 Platform SE v1.4.2)Java Code:Summary of regular-expression constructs Construct Matches Characters x The character x \\ The backslash character \0n The character with octal value 0n (0 <= n <= 7) \0nn The character with octal value 0nn (0 <= n <= 7) \0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7) ...
Then figure out how to write that as a string literal.
Hint: double escaping required.
-
Re: Problem with replace word
Or how about simply using replace(...) rather than replaceAll(...) since the former doesn't use regex, and regex isn't really needed here for this simple replace?
- 12-11-2011, 06:10 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Find a text in a file and write a word following this word
By lemontree45 in forum New To JavaReplies: 3Last Post: 08-30-2011, 04:44 PM -
String.replace ?!
By HearT.Hunt3r in forum New To JavaReplies: 4Last Post: 08-22-2011, 03:04 AM -
Word problem...PROBLEM!
By aceoraiz in forum Threads and SynchronizationReplies: 6Last Post: 12-17-2010, 03:38 AM -
how to replace word in netbeans 6.9.1
By enggvijaysingh@gmail.com in forum New To JavaReplies: 1Last Post: 11-23-2010, 03:32 PM -
string replace problem
By soni in forum Advanced JavaReplies: 8Last Post: 07-06-2008, 01:21 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks