Results 1 to 6 of 6

Thread: Replace backslash in a string

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Replace backslash in a string

    Hello
    I must insert in my database for a string containing the backslash \ and when reregistration in ABSE data s nothing displays.
    So I try to do this:
    Code:
    Code:
    myText.replaceAll("\\","\\\\");
    Its returns me a syntax error, how to provide a basis to understand my backslash? Any help on this topic will interest me.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Replace backslash in a string

    Hello,
    If I am not wrong then you are getting the following error.
    Code:
    java.util.regex.PatternSyntaxException: 
    Unexpected internal error near index 1
    \
    If you want to remove this error from getting when you compile the code, then you have to modify your code.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Replace backslash in a string

    Hello,
    Just in case you do this well in your code:
    Code:
    myText = myText.replaceAll("\\", "\\\\");
    Because the method replaceAll returns the modified string, and does not alter that which is given as parameter. I hope you are getting the point what I am trying to explain you. If you want more information on this , then you can go to the sun's official site for java and there you can find more detailed information on this.

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Replace backslash in a string

    Hello,
    Even I was trying a similar kind of a program , And even i get a same error , i have no idea what this error means. So, please if you have any idea then even i am the interested candidate.
    Here is what I get
    Code:
    java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    \
     ^
    at java.util.regex.Pattern.error(Unknown Source)
    	at java.util.regex.Pattern.compiles(Unknown Source)
    	at java.util.regex.Pattern. <init>(Unknown Source)
    	at java.util.regex.Pattern.compiles(Unknown Source)
    	at java.lang.String.replaceAll(Unknown Source)

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Replace backslash in a string

    Hello,
    I have a code with me and i think you guys will be interested to see this
    Here is the code:
    Code:
     public class Replace  
     {  
        public static void main(String[] args)  
       {  
           String str = "abc";  
          String rep = str.replaceAll("abc", "c:\\\\test\\\\abc");  
           System.out.println(rep);  
        }  
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Replace backslash in a string

    Hello,
    I think the most important in such programs is to do the loop, what I am saying is this
    Code:
    loop check:
    for (int i=0; i<inputString.length(); i++){
    char chval = inputString.charAt(i);
    if (chval=='\\'){
    return false;
    }
    }
    Hope this will help you.

Similar Threads

  1. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM
  2. Find and Replace in JavaScript String
    By Wilbur in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:23 PM
  3. Simple string replace in php
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:37 PM
  4. Replace backslash with forward slash in Perl
    By _Gentoo_Nile_ in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 10:02 PM
  5. Replace a string in multiple files Linux
    By ADISH in forum Tips & Tweaks
    Replies: 0
    Last Post: 27-06-2009, 10:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,412,904.55466 seconds with 17 queries