4 ms·
The difference between Python and Java is that the following (Python): text = """hello world""" will contain the intendation before world, w
by looperhacks 6y ago
The difference between Python and Java is that the following (Python):
text = """hello
world"""
will contain the intendation before world, while in Java:
String text = """hello
world"""
will be equivalent to "hello\nworld".
- deleted 6y ago[deleted]
- masklinn 6y agoFWIW the Java snippet seems to be a syntax error instead, the JEP explicitly defines the opening delimiter as three double quotes, any amount of whitespace, and a line terminator. The closing delimiter can, however, be on the same line as the final content (in order to suppress the trailing newline for instance).
- ygra 6y agoThe latter is not valid Java, as the """ must not be followed by string contents. And var text = """ hello world"""; will not have any indentation, as the common indentation is stripped from all lines.
- apk-d 6y agoI wish that's how C# handled it! There are verbatim string literals that support newlines, but in practice, most of the time I see people stick to regular string concatenation because it ruins code indentation/alignment. That's how Rider chooses to refactor code when you insert a newline as well. Multi-line verbatim strings begin to get useful around the point where you should be probably loading your text from some resource instead of embedding it in the source.
- ygra 6y agoThere's plenty of contexts where the extra indentation doesn't matter, though. And C#'s verbatim strings, at least when it comes to multiple lines, have the benefit of being extremely predictable in what ends up in the string (everything, including whitespace). They also have other uses that Java doesn't even address, like not interpreting escape sequences inside the string. I'm not sure you can really compare them. They're two different features with fairly different uses, it's just that one or two of those uses somewhat overlap.
- apk-d 6y agoThey're comparable in the sense that I'm guessing C# is unlikely to get yet another string declaration syntax, so we're stuck with what we've got. Regarding predictability I definitely agree, but good syntax highlighting would help with that.