Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin add carriage return into multiline string

In Kotlin, when I build a multiline string like this:

value expected = """
                |digraph Test {
                |${'\t'}Empty1;
                |${'\t'}Empty2;
                |}
                |""".trimMargin()

I see that the string lacks carriage return characters (ASCII code 13) when I output it via:

println("Expected bytes")
println(expected.toByteArray().contentToString())

Output:

Expected bytes
[100, 105, 103, 114, 97, 112, 104, 32, 84, 101, 115, 116, 32, 123, 10, 9, 69, 109, 112, 116, 121, 49, 59, 10, 9, 69, 109, 112, 116, 121, 50, 59, 10, 125, 10]

When some code I'm trying to unit test builds the same String via a PrintWriter it delineates lines via the lineSeparator property:

/* 
 * Line separator string.  This is the value of the line.separator
 * property at the moment that the stream was created.
 */

So I end up with a string which looks the same in output, but is composed of different bytes and thus is not equal:

Actual bytes
[100, 105, 103, 114, 97, 112, 104, 32, 84, 101, 115, 116, 32, 123, 13, 10, 9, 69, 109, 112, 116, 121, 49, 59, 13, 10, 9, 69, 109, 112, 116, 121, 50, 59, 13, 10, 125, 13, 10]

Is there a better way to address this during string declaration than splitting my multiline string into concatenated stringlets which can each be suffixed with char(13)?

Alternately, I'd like to do something like:

value expected = """
                |digraph Test {
                |${'\t'}Empty1;
                |${'\t'}Empty2;
                |}
                |""".trimMargin().useLineSeparator(System.getProperty("line.separator"))

or .replaceAll() or such.

Does any standard method exist, or should I add my own extension function to String?

like image 882
Tom Tresansky Avatar asked Feb 22 '18 17:02

Tom Tresansky


People also ask

How do you add a line break in Kotlin?

To split a string on newlines, we can use the regular expression \r?\ n|\r which matches with all different line terminator i.e., \r\n , \r , and \n . To skip empty lines, we can change the regular expression to [\r\n]+ . To match with any Unicode linebreak sequence, we can use the linebreak matcher \R .

How do you split a string in Kotlin?

Using String's split() function The standard solution to split a string in Kotlin is with the native split() function, which takes one or more delimiters as an argument and splits the string around occurrences of the specified delimiters. The split() function returns a list of strings.

How do you split a string with space in Kotlin?

We can use the split() function to split a char sequence around matches of a regular expression. To split on whitespace characters, we can use the regex '\s' that denotes a whitespace character. Note that we have called the toTypedArray() function, since the split() function returns a list.

How do I concatenate strings in Kotlin?

Using + Operator The + operator is one of the widely used approaches to concatenate two strings in Kotlin. Alternatively, you can use the plus() function to concatenate one string at the end of another string.


1 Answers

This did the trick.

    System.lineSeparator()
like image 119
Ahamed Mujeeb Avatar answered Sep 24 '22 21:09

Ahamed Mujeeb