0

In my Scala web app I am parsing a URL parameter which normally contains hyphens. In some instances the parameter is coming through with non-ascii hyphens, i.e.

11157‑007‑9120‑9

instead of

11157-007-9120-9

This is causing a downstream system to explode...

What is the best way of converting these non-ascii characters? There are other non-ascii characters as well as the hyphen...

laura
  • 2,951
  • 9
  • 44
  • 61
  • Are there ONLY hyphens to be put in the String? Use `replaceAll()` – Georgian Jan 07 '13 at 14:37
  • If I am not mistaken, those characters are not legal in a URI (therefore in a URL), right? – fge Jan 07 '13 at 14:38
  • make sure how the URL is encoded, supposedly there is a mismatch and therefore you are getting strange characters ... (e.g. URL is UTF and you are reading it as ASCII) – xhudik Jan 07 '13 at 14:39
  • I think it is caused by a user manually typing in the parameter. (Aren't query parameters encoded differently from the url?) I think it is likely just to be the hyphen. It looks fine when we get it - we have to use emacs to see the problem chars, and it works fine in our system - it is just the downstream system that can't handle it... – laura Jan 07 '13 at 14:54
  • 1
    @laura hmm in this case I think the easiest solution would be to test whether the user is inputting correct data (valid URL). In other words, test if the string is pure ASCII (or some special chars if permitted), if not - throw exception/error – xhudik Jan 07 '13 at 14:59
  • Thanks for the input. It's a most frustrating bug! – laura Jan 07 '13 at 15:42

1 Answers1

0

If you only want to replace the hyphens, you can just use replaceAll.

val strangeHyphen:Char = '\u1234' //replace this with the non-ASCII hyphen character
val newStr = str.replaceAll(stangeHyphen, '-')
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142