I want to have two different type of newline so I can print a string which looks like it has multiple newlines but gives me a way to differentiate between them. I want to have string with newlines in them and look them up using RegularExpresssion["...\n...\r..."]->.... Basically there will be a huge string with mix of hard and short returns then I will have a bunch of StringReplace[...] where \n will be captured and replace by something and \r by something else.

  • Is there a way to have hard and soft returns in a Mathematica string?
  • If I should write a string like "sdfsdf\nadsad\rasdasd", is there a function, say StringPrint["sdfsdf\nadsad\rasdasd"], which will convert the "\n" and "\r" to their print form? Note: For this I want to do inline evaluation by first selecting and pressing Ctrl+Shift+Enter not Shift+Enter evaluation.

Enter is the keybinding for \[Newline] which is \n but since Shift+Enter is reserved for evaluation while it is a standard key for soft return \r. If \[Newline] equivalent for \r can be found that would still make the life easier as well.

  • 2
    $\begingroup$ I don't really understand. Simply Printing a string (or returning it from an evaluation) with line ending characters will show those characters as line breaks. $\endgroup$ – Sjoerd Smit Mar 24 '19 at 10:29
  • $\begingroup$ I think I found it \:000a is the hex way to enter \n and \:000d is the hex way to enter \r and I think hex way might be the only way to input in print form \r. \n already has a key binding Enter and various aliases like \[Newline], \[RawReturn], \[ParagraphSeparator] etc , does \r also has some hidden keybinding or aliases? $\endgroup$ – user13892 Mar 24 '19 at 12:08
  • $\begingroup$ Oh, wait. I think I get it. Can't you just escape the backslash? I.e., StringReplace[str, {"\r" -> "\\r", "\n"-> "\\n"}] $\endgroup$ – Sjoerd Smit Mar 24 '19 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.