How to Make Multiple Line String to Single Line String

how to make multiple line string to single line string?

If you want to use regex, you should use the String.replaceAll() method.

Change Multi line strings to Single line

I have a simple way for this. You can do this without extra code. Just write like this -

var str = "12345,\
234234,\
234324,\
234324,\
234324,\
234234";

now just add slash

Ok, If you don't want to use above method then use another plan is -

take inside array and after that use join method

var str = [12345,
234234,
234324,
234324,
234324,
234234];


str.join(",");

If we are using ES6, Then we have a elegant way to do this using Backtick -

var str = `12345,
234234,
234324,
234324,
234324,
234234`;

convert multi line string to single line

I tried with \n and it works

scala> val myString = """
| a=b
| c=d
| """.stripMargin
myString: String =
"
a=b
c=d
"

scala> myString.replaceAll("\n",";")
res0: String = ;a=b;c=d;

scala> res0.substring(1, myString.length)
res1: String = a=b;c=d;

I hope it helps

python convert multiline to single line

Deleted my previous answer because I realized it was wrong and I needed to test this solution.

Assuming that you are reading this from the file, you can do the following:

f = open('test.txt', 'r')
lines = f.readlines()
mystr = '\t'.join([line.strip() for line in lines])

As ep0 said, the ^M represents '\r', which the carriage return character in Windows. It is surprising that you would have ^M at the beginning of each line since the windows new-line character is \r\n. Having ^M at the beginning of the line indicates that your file contains \n\r instead.

Regardless, the code above makes use of a list comprehension to loop over each of the lines read from test.txt. For each line in lines, we call str.strip() to remove any whitespace and non-printing characters from the ENDS of each line. Finally, we call '\t'.join() on the resulting list to insert tabs.

Convert multiline string in a variable in bash to a single-line string with newlines escaped

Bash has two built-in ways to quote values suitable for re-ingestion. These will handle not only newlines but also tabs, quotes, and backslashes:

❯ echo "${str@Q}"
$'\nHello\nWorld\n'
❯ printf '%q\n' "$str"
$'\nHello\nWorld\n'

Alternatively, if you simply want to replace newlines and nothing else you can use ${var//search/replace} syntax to do replacements:

❯ echo "${str//$'\n'/\\n}"
\nHello\nWorld\n

Multi-Line String to Single-Line String conversion in PowerShell

# create a temp file that looks like your content
# add the A,B,C,etc to each line so we can see them being joined later
"Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Gxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | Set-Content -Path "$($env:TEMP)\JoinChunks.txt"

# read the file content as one big chunk of text (rather than an array of lines
$textChunk = Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw

# split the text into an array of lines
# the regex "(\r*\n){2,}" means 'split the whole text into an array where there are two or more linefeeds
$chunksToJoin = $textChunk -split "(\r*\n){2,}"

# remove linefeeds for each section and output the contents
$chunksToJoin -replace '\r*\n', ''

# one line equivalent of above
((Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw) -split "(\r*\n){2,}") -replace '\r*\n', ''

How to write a single line string literal across multiple lines in the source code

Use ~Newline directive in format.

(string-equal (format nil "multi-~
line ~
pine~
apple")
"multi-line pineapple")


Related Topics



Leave a reply



Submit