I found it will generate the message, but also gives the right result in this case.

StringJoin::string: String expected at position 2 in <>x<>. >>

I can Use Quiet or Ignore the message, or Add one ToString to x


Which is the right choice in such a case?

More elegant codes?

  • $\begingroup$ Please use descriptive titles for your questions instead of "Little problem with X" $\endgroup$ – rm -rf Jun 14 '13 at 5:12
  • $\begingroup$ I have closed this question as a duplicate. That question gets into a deeper issue of infinite evaluation but up front the problem is the same, use of Rule rather than RuleDelayed, and it is even in the context of String operations. If you disagree with this closure please respond to this comment. $\endgroup$ – Mr.Wizard Jun 14 '13 at 5:47
  • $\begingroup$ @Mr.Wizard ok, no problem. $\endgroup$ – HyperGroups Jun 14 '13 at 5:56

Well, I think this problem is related to the evaluation process of Rule. When performing a replacement with a named pattern (x_String in this case), you'd better use RuleDelayed rather than Rule:

names = Select[(StringCases[list2 = Names["System`*"], 
  RegularExpression["[a-z]+"], IgnoreCase -> True] // Flatten), 
   MemberQ[list2, #] &];

namesReplaceRules = names /. x_String :> (x -> "`" <> x <> "`");

Note that even if you use Rule, you can still get the right result, this is because x -> "`" <> x <> "`" will evaluate to itself with a error message. But if the rhs of Rule evaluate to something else, something bad would happen.

  • $\begingroup$ nice+1, I mush have tried RuleDelayed incompletely. :( $\endgroup$ – HyperGroups Jun 14 '13 at 4:53
  • $\begingroup$ How do you think my formatting here? Actually I like MrWizard's style one yellow blockquotes for output. mathematica.stackexchange.com/questions/26974/… $\endgroup$ – HyperGroups Jun 14 '13 at 5:11
  • $\begingroup$ @HyperGroups I don't really know, I like Leonid Shifrin's style, with codes formatted neatly. $\endgroup$ – mmjang Jun 14 '13 at 5:24
  • $\begingroup$ en, I learned that (newline ==> newline),fine. I added the input numbers for indicating possible Picture results, because rm-rf said that the image uploader would not be fixed in a short time. So I combine several picture results in one results to insert in the post more convinently. mathematica.stackexchange.com/a/26932/6648 $\endgroup$ – HyperGroups Jun 14 '13 at 5:34

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