Home > database >  Issue with replacing part of string in Java
Issue with replacing part of string in Java

Time:03-25

I have a method that reads a sql query as a string and needs to replace all '\' with '\\'. This is done to prevent \n and \r being processed as line breaks in the output.

private static String cleanTokenForCsv(String inputToken) {
        if (inputToken == null) {
            return "";
        }
        if (inputToken.length() == 0 || inputToken.contains(",") || inputToken.contains("\"")
                || inputToken.contains("\n") || inputToken.contains("\r")) {
            String replacedToken = inputToken.replace(",", ";");
            return String.format("\"%s\"", replacedToken.replace("\"", "\"\""));
        } else {
            return inputToken;
        }
    }

Sample Input

(\nSELECT\n a.population_id\n ,a.empi_id\n\r ,a.encounter_id\n ,SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value),'|',2)::FLOAT as earliest_temperature\nFROM\n ph_f_result a)

The expected output for the query would be along the lines of

"(\nSELECT\n a.population_id\n ;a.empi_id\n\r ;a.encounter_id\n ;SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value);'|';2)::FLOAT as earliest_temperature\nFROM\n ph_f_result a)"

The entire query in one line with the line breaks intact

However, the output instead is

"(
SELECT
    a.population_id
    ;a.empi_id

    ;a.encounter_id
    ;SPLIT_PART(MIN(a.service_date||'|'||a.norm_numeric_value);'|';2)::FLOAT as earliest_temperature
FROM
    ph_f_result a)"

I also tried the following:

replacedToken = replacedToken.replace("\\", "\\\\");

With Regex

replacedToken = replacedToken.replaceAll("\\\\", "\\\\\\\\");

Edit: So I can get it to work if I add individual replace calls for \n and \r like below

replacedToken = replacedToken.replace("\n","\\n"); 
replacedToken = replacedToken.replace("\r", "\\r");

But I am looking for something more generic for all '\n' instances

CodePudding user response:

You have carriage return and newline characters, not escaped r or n.

replacedToken = replacedToken.replace("\n", "\\n").replace("\r", "\\r");

This replaces all carriage return and newline characters with their escaped equivalents.

CodePudding user response:

I assume that your goal is simply to convert characters \r, \t and \n in an input String to double-quoted two-character strings "\\r" and so on, so that printing the string does not result in newlines or tabs.

Note that the character \n does not really contain the character \ at all. We simply agree to write \n to represent it. This should work:

   public static String escapeWhitespaceEscapes(String input) {
       return input
           .replace("\n", "\\n")
           .replace("\r", "\\r")
           .replace("\t", "\\t");
   }

But note that you will have to perform the reverse operation to get back the original string.

CodePudding user response:

Try using
System.out.println(data.replace("\n","\n"));

  • Related