1

While trying to build my own simple C-language highlighter for strings in Mathematica (see here for a very sophisticated highlighter generator package by Leonid Shifrin) I came upon the issue of using \n (line delimiter) as a delimiter in StringSplit. Manually delimiting a string with \n or formatting in the front-end (as done below) both result in a string delimited with \n at the appropriate place as can be seen by looking at the FullForm.

str = "Hello World 
2016"  
(* Hello World
   2016 *)

str // FullForm 
(*Hello World \n2016*)

This is however not recognized by StringSplit:

StringSplit[str, {Whitespace -> "Whitespace", "\n" -> "Newline"}] 
(* {"Hello", "Whitespace", "World", "Whitespace", "2016"} *)

The expected output would be:

{"Hello", "Whitespace", "World", "Whitespace", "Newline", "2016"}

Is there a special symbol like Whitespace or any other way to represent \n so that StringSplit accepts it as a delimiter?

Sascha
  • 8,459
  • 2
  • 32
  • 66
  • 1
    Take a look at what Whitespace ref page says. – Kuba Feb 29 '16 at 19:59
  • 3
    StringSplit[str, {" " .. -> "Whitespace", "\n" -> "Newline"}] – Dr. belisarius Feb 29 '16 at 19:59
  • @Kuba, Thanks! In the light of the clear documentation on Whitespace this turned out to be a stupid question ...once again an example of dangerous "intuition" and the suggestive nature of the naming scheme of build-in symbols – Sascha Feb 29 '16 at 20:04
  • StringSplit["abba", "b"] gives empty string from between "b"s. Shouldn't it be the case for your " \\n"? If so, maybe: {"\n" -> "Newline", WhitespaceCharacter -> "Whitespace"}? – Kuba Feb 29 '16 at 20:05

0 Answers0