Sandeep K Gujje
Sandeep K Gujje

Reputation: 35

Replacing escape characters with a space

I have been asked to replace a escape characters in a string with a space.

I have been trying like below

String replace(String data)
    {
        String strSpecial = "";
        strSpecial = " \f\n\r\t\'\"\\";     

        char charArray[] = strSpecial.toCharArray();
        String strOutData = data;
        for (int i =0;i<charArray.length;i++)
        {  
              strOutData = strOutData.replace(charArray[i], ' ');
    }       
        return(strOutData);

    }

Im getting the error - "Unterminated String Constant"

It is working when I use any character instead of a space to replace the escape character.

Please suggest

Thanks Sandeep

Upvotes: 1

Views: 2478

Answers (6)

Costas Vrahimis
Costas Vrahimis

Reputation: 539

I wrote this in notepad because I am not at my machine so I am not 100% that this works but you can try it. What I can tell you is that if you do not want to use regex you should use the string builder to replace characters in a string because strings are immutable in java so your code has to return a new string every time you want to do a replace, which could take a while depending on how many characters have to be replaced in data.

 public string replace(String data)
 {
     String strSpecial = "";
     strSpecial = " \f\n\r\t\'\"\\";
     StringBuilder sb = new StringBuilder();     

     char charArray[] = strSpecial.toCharArray();
     String strOutData = data;

     for (int i =0;i<data.length();i++)
     {  
         for(int j=0; j < charArray.lenght(); j++)
         {
             if(strOutData.charAt(i) == charArray[j])
                 sb.append(' ');
             else
                 sb.append(strOutData.charAt(i));
         }
     }       
         return(sb.toString());


 }

Upvotes: 2

hatellla
hatellla

Reputation: 5132

I ran your code and its running fine. This is the code that I ran

public static void main(String args[]) {

    String strSpecial = "";
    strSpecial = " \f\n\r\t\'\"\\";     

    char charArray[] = strSpecial.toCharArray();
    String strOutData = "S\t\"\'\r\f\n\n\'J";
    for (int i =0;i<charArray.length;i++)
    {  
          strOutData = strOutData.replace(charArray[i], ' ');
}       
    System.out.println(strOutData);
} 

Can you provide me the input that you are trying with? Although, from my side, right way to do this thing is using regular expression

Upvotes: 0

Raju Sharma
Raju Sharma

Reputation: 2516

Your code will process characters which are reserved in java like:'\' , it will show no termination of string if we use '\' without any preferred method like "\"" .

Try this:

String replace(String data)
    {
        String strSpecial = "";
        strSpecial = " \f\n\r\t\'\"\\";     


        return(strSpecial.replace("\"", " "));

    }

Upvotes: 0

user2883797
user2883797

Reputation: 23

Why do you think it is not working? I used the following to test and it worked.

    string strSpecial = " \f\n\r\t\'\"\\";
    string strtmp2 = strSpecial.Replace('\f', ' ').Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' ').Replace('\'', ' ').Replace('\"', ' ').Replace('\\', ' ');

Upvotes: 0

Arcturus
Arcturus

Reputation: 27055

As far as I can see, there is nothing wrong with your method.

Check your calling JSP code. If you use this inside a Javascript function, check if the parsed Javascript code still makes sense.

The easiest way to do this is to just inspect the resulting HTML. Perhaps you might find your problem when you examine that.

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

I would use regex:

String replace(String data) {
    return data.replaceAll("[\f\n\r\t\'\"\\\\]", " ");
}

Note the 4 backslashes required to achieve a single literal regex backslash; each pair of backslashes in the String literal makes a single literal backslash in the String, and the resulting pair of backslashes in the String makes a single literal backslash in the regex.

Upvotes: 3

Related Questions