# ToString get local name of the symbol

I am using ToString to convert symbols to a string. So my entire code is written assuming ToString[a] will return "a". Now I am converting my notebook to Mathematica package. Now ToString[a] returns "PackagenamePrivatea". This breaks my assumptions and so my code breaks;

Is there anyway to tweak like ToStringSafe[a] that returns a e.g. the local name of that symbol. Not the global one.

• Take a look at SymbolName – Kuba Oct 25 '17 at 11:47
• related: 32223, 15687, 119922, 17916 – Kuba Oct 25 '17 at 11:51

You can use SymbolName.

foobar
(* foobar *)

ToString[foobar]
(* "foobar" *)

SymbolName[foobar]
(* "bar" *)
`

Warning:

Converting between symbols and strings requires great care, especially when different contexts are involved. Make sure you really need to do this and that you really do not want the fully qualified name. Robust code will not normally convert between strings and symbols.

• Sorry I didn't understand the warning. What did you mean by different contexts ? Did you mean different scopes or different namespaces ? – Neel Basu Oct 25 '17 at 12:31
• @NeelBasu There is no such thing as "local name" or "global name". When you used these words I started to wonder if you have a good understanding of contexts and name resolution. Contexts are kind of like namespaces but not exactly. – Szabolcs Oct 25 '17 at 12:57