3
$\begingroup$

I have two expressions which are exactly same:

StringReplace["in the next few days", {"in" -> "ffdsf"}]

and

StringReplace["in the next few days", {"in­" -> "ffdsf"}]

However,the first one evaluated to "ffdsf the next few days" while the second one remained "in the next few days"

By Trace I have

StringReplace["in the next few days", {"in" -> "ffdsf"}] // Trace

{{{\!\(\*
TagBox[
RowBox[{"\"\<in\>\"", "->", "\"\<ffdsf\>\""}],
HoldForm]\), \!\(\*
TagBox[
RowBox[{"\"\<in\>\"", "->", "\"\<ffdsf\>\""}],
HoldForm]\)}, \!\(\*
TagBox[
RowBox[{"{", 
RowBox[{"\"\<in\>\"", "->", "\"\<ffdsf\>\""}], "}"}],
HoldForm]\)}, \!\(\*
TagBox[
RowBox[{"StringReplace", "[", 
RowBox[{"\"\<in the next few days\>\"", ",", 
RowBox[{"{", 
RowBox[{"\"\<in\>\"", "->", "\"\<ffdsf\>\""}], "}"}]}], "]"}],
HoldForm]\), \!\(\*
TagBox["\"\<ffdsf the next few days\>\"",
HoldForm]\)}

and

StringReplace["in the next few days", {"in­" -> "ffdsf"}] // Trace

{{{\!\(\*
TagBox[
RowBox[{"\<\"in­\"\>", "->", "\<\"ffdsf\"\>"}],
HoldForm]\), \!\(\*
TagBox[
RowBox[{"\<\"in­\"\>", "->", "\<\"ffdsf\"\>"}],
HoldForm]\)}, \!\(\*
TagBox[
RowBox[{"{", 
RowBox[{"\<\"in­\"\>", "->", "\<\"ffdsf\"\>"}], "}"}],
HoldForm]\)}, \!\(\*
TagBox[
RowBox[{"StringReplace", "[", 
RowBox[{"\<\"in the next few days\"\>", ",", 
RowBox[{"{", 
RowBox[{"\<\"in­\"\>", "->", "\<\"ffdsf\"\>"}], "}"}]}], "]"}],
HoldForm]\), \!\(\*
TagBox["\<\"in the next few days\"\>",
HoldForm]\)}

What happened? Why the second StringReplace didn't evaluate?

I'm using Mathematica10.0.2 on windows7, the problem remains after restarting Mathematica. copy this StringReplace["in the next few days", {"in" -> "ffdsf"}] and this StringReplace["in the next few days", {"in­" -> "ffdsf"}] from this website to notebook and then they would evaluate differently.

after open the notebook by Notepad++, I found the clue. The abnormal expression, the one didn't evaluate, looks like

Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"StringReplace", "[", 
RowBox[{"\"\<in the next few days\>\"", ",", " ", 
RowBox[{"{", 
RowBox[{
"\"\<in\[DiscretionaryHyphen]\>\"", " ", "->", " ", "\"\<ffdsf\>\""}], 
"}"}]}], "]"}]], "Input"],

Cell[BoxData["\<\"in the next few days\"\>"], "Output",
CellChangeTimes->{3.676188472550608*^9}]
}, Open  ]]

in Notepad++, while the normal expression, looks like

Cell[CellGroupData[{

Cell[BoxData[
RowBox[{"StringReplace", "[", 
RowBox[{"\"\<in the next few days\>\"", ",", 
RowBox[{"{", 
RowBox[{"\"\<in\>\"", "\[Rule]", "\"\<ffdsf\>\""}], "}"}]}], 
"]"}]], "Input"],

Cell[BoxData["\<\"ffdsf the next few days\"\>"], "Output",
CellChangeTimes->{3.676188474586664*^9}]
}, Open  ]]

copy the above two Cells into notebook then they looks exactly same(in the mathematica notebook), while the first one didn't evaluated correctly.

$\endgroup$
  • $\begingroup$ With questions like this, you must give complete information to reproduce the problem, including: 1. is this evaluated after a kernel restart? Try to give complete instructions to reproduce it. 2. what version of Mathematica, including release number? 3. what OS? $\endgroup$ – Szabolcs Jun 29 '16 at 11:10
  • $\begingroup$ I cannot reproduce in 9.0.1, 10.0.2, 10.3.1 or 10.4.1 on OS X. $\endgroup$ – Szabolcs Jun 29 '16 at 11:12
  • $\begingroup$ I do not reproduce with versions 5.2, 8.0.4 and 10.4.1 on Win7 x64. $\endgroup$ – Alexey Popkov Jun 29 '16 at 11:15
  • 3
    $\begingroup$ do not type expression in notebook, copy them respectively from this website. @Szabolcs $\endgroup$ – Harry Jun 29 '16 at 11:19
  • $\begingroup$ You twice say "from this website". This website is a huge collection of code. Do you think you could be a little more explicit about where on this website? $\endgroup$ – Eric Towers Jun 29 '16 at 15:31
6
$\begingroup$

On the second line, the string that looks like "in" is in fact a three-character string. The third character is a soft-hyphen: it is invisible and it is used to mark a position where a word can be hyphenated. It has character code 173.

StringReplace finds no match due to this invisible third character.

You can detect this using FullForm or ToCharacterCode.

enter image description here

$\endgroup$
  • $\begingroup$ I will kill the one who invented this damn character...are they any similar invisible characters? $\endgroup$ – Harry Jun 29 '16 at 12:01
  • 1
    $\begingroup$ This problem is just like those involving stupid Boxes in a normal expression. extremely hard to detect...... $\endgroup$ – Wjx Jun 29 '16 at 12:18
  • $\begingroup$ @Harry Yes, there are a few. I love this one because it lets me have properly justified ebooks with good hyphenation ;-) Another one is the zero width space. Some more mentioned here: en.wikipedia.org/wiki/Unicode_control_characters There are also characters which look the same, yet they are different. An example is space vs non-breaking space. Another one is capital Roman A vs capital Greek Alpha, Α. $\endgroup$ – Szabolcs Jun 29 '16 at 12:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.