Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 599507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:30:22+00:00 2026-05-13T16:30:22+00:00

I want multi-line strings in java, so I seek a simple preprocessor to convert

  • 0

I want multi-line strings in java, so I seek a simple preprocessor to convert C-style multi-lines into single lines with a literal ‘\n’.

Before:

    System.out.println("convert trailing backslashes\
this is on another line\
\
\
above are two blank lines\
But don't convert non-trailing backslashes, like: \"\t\" and \'\\\'");

After:

     System.out.println("convert trailing backslashes\nthis is on another line\n\n\nabove are two blank lines\nBut don't convert non-trailing backslashes, like: \"\t\" and \'\\\'");

I thought sed would do it well, but sed is line-based, so replacing the ‘\’ and the newline that follows it (effectively joining the two lines) is not very natural in sed. I adapted sredden79’s oneliner to the following – it works, it’s clever, but it’s not clear:

sed ':a { $!N; s/\\\n/\\n/; ta }'

The substitute is of escaped literal backslash, newline with escaped literal backslash, n. :a is a label and ta is goto label if the substitute found a match; $ means the last line, and $! is the opposite (i.e. all lines but the last). N means to append the next line to the pattern space (thus making the \n character visible.)

EDIT here’s a variation to keep compiler error line numbers etc accurate: it turns each extended line into "..."+\n (and handles the first and last lines of the String correctly):

sed ':a { $!N; s/\\\n/\\n"+\n"/; ta }'

giving:

    System.out.println("convert trailing backslashes\n"+
"this is on another line\n"+
"\n"+
"\n"+
"above are two blank lines\n"+
"But don't convert non-trailing backslashes, like: \"\t\" and \'\\\'");

EDIT Actually, it would be better have Perl/Python style multi-line, where it starts and ends with a special code on one line (“”” for python, I think).

Is there a simpler, saner, clearer way (maybe not using sed)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:30:22+00:00Added an answer on May 13, 2026 at 4:30 pm

    A perl one-liner:

    perl -0777 -pe 's/\\\n/\\n/g'
    

    This will read either stdin or the file(s) named after it on the command line and write the output to stdout.

    If you’re using an editor that supports filtering, like vi or emacs, just filter your text through the above command and you’re done:

    If you’re using Windows and have to worry about \r :

    C:\> perl -0777 -pe "s/\\\r?\n/\\n/g"
    

    although I think win32 Perl handles \r itself so this may be unnecessary.

    The -0777 option is a special case of the -0 (that’s a zero) option that defines the line or record separator. In this case, it means that we don’t want any separator so read the entire file in as a single string.

    The -pe option is a combination of -p (process line-by-line and print the result) and -e (next argument is (a line of) the program to execute)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(For just multi-line strings in Java, see: Java multiline string . This question below
Python's string-literal juxtaposition makes multi-line strings much easier and prettier to write, but when
I have preformatted strings with line-breaks and multi-spaces and I want to append them
I want to parse out all lines from a multi-line string up until the
I want to indent multi-line in 'vim/gvim', is there any shortcut in the vim/gvim?
I've got a multi line text file that I want to use to create
I want to control the x and y axis of a multi series line
I have a multi-line string like this: ...Togo...Togo...Togo...ACTIVE... I want to get everything between
I want to remove all whitespace characters from a multi-line string using regex. What
I have a multi-line string that I want to do an operation on each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.