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

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Change $error = substr($row['error'],0,32) + "..."; to: $error = substr($row['error'],0,32)… May 16, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer I like it: class Array def to_weird_hash length == 1… May 16, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer Yes, this is right approach. Assertion is OK for internal… May 16, 2026 at 5:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

If someone posts a multi-line post that contained text and links, I want to
In java i would like to read a file line by line and print
I have a Java project which will include a number of large SQL statements
I'm learning Scala, so this is probably pretty noob-irific. I want to have a
Can anyone point me to a program that strips off strings from C source
Please help me solve my big problem. in my on-line shopping project i created
I need to pull RAM information from several cpuz reports and put them into
I'm attempting to extract a given pattern within a text file, however, the results
I've got a program I would like to use to input a password and
Is it possible using NSArrayController to bind a NSTextField 's value to a particular

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.