-1

Is there a way in JavaScript to display a symbol similar to '#' in a string such that "Enter #time to check time".split('#') doesn't break it into pieces?

It should return a complete string instead of ['Enter ','time to check time'].

I tried using this '#️⃣' symbol in place of '#' but it still somehow gets detected. On further debugging I found out that this emoji is composed of 2 code points out of which 1st one translates to unicode \u0023 which is similar to that of the '#' symbol. And hence it splits the string.

This might seem like a stupid issue but the underlying use case is very interesting but unfortunately I could not share here due to confidential reasons. So, any kind of help is highly appreciated.

Thanks!

Deepak Terse
  • 652
  • 8
  • 30
  • Instead of `includes('#')` test for a match with a regular expression like `/#[A-Za-z]+/` or something. If typical hashtags are not supposed to be detected as containing a `#` how exactly will the character be part of the string? Is there going to be whitespace after it? –  Dec 02 '21 at 19:30
  • 2
    Why exactly do you do `.split('#')` in the first place? – Sebastian Simon Dec 02 '21 at 19:39
  • @ChrisG thanks for the answer but it doesn't fulfill the use case. I've updated the question to show the actual problem statement – Deepak Terse Dec 02 '21 at 19:40
  • @SebastianSimon updated the question. Please do have a look – Deepak Terse Dec 02 '21 at 19:40
  • 1
    @DeepakTerse I don’t see how your edit answers my question. _Why_ is the string split by `#`? Why is the string not left unmodified? What is the purpose of splitting? Can’t you find a different character to split on, or, better yet, a different string format, e.g. a more flexible one like JSON? – Sebastian Simon Dec 02 '21 at 19:46
  • 1
    Try `#` (U+FF03, *Fullwidth Number Sign*) instead of `#️⃣` which is composed of *3* code points `#` (U+0023, *Number Sign*) - `️` (U+FE0F, *Variation Selector-16*) - `⃣` (U+20E3, *Combining Enclosing Keycap*). – JosefZ Dec 02 '21 at 19:49
  • 1
    So basically, this string is part of an action where we say something like `print#$current_time` or `print#$message`. and in this case the action is `print#Enter #time to check time`. So this splits the action into 3 parts. But actually there are 2 parts - action name and the parameter – Deepak Terse Dec 02 '21 at 19:52
  • @SebastianSimon And like you mentioned the underlying logic can be changed to support the '#' but I'm looking for any solution in the content itself – Deepak Terse Dec 02 '21 at 19:53
  • @DeepakTerse In that case, don't go looking for a different character to split by - instead just split it into only two parts! `const parts = string.split('#'); const action = parts.shift(); const parameter = parts.join('#');` being a common solution to that. Or use string methods for working with indices. – Bergi Dec 02 '21 at 19:53
  • @JosefZ ur solution works. Thanks! – Deepak Terse Dec 02 '21 at 20:40

1 Answers1

-1

The solution here was to switch to full-width hash symbol which has a different unicode U+FF03 whereas half-width hash symbol (which is the same as the hash symbol on the keyboard) has a unicode U+0023

Deepak Terse
  • 652
  • 8
  • 30
  • 1
    This is not a solution, it's at best a workaround. Any of the two characters can still appear as a parameter. – Bergi Dec 02 '21 at 20:57