Replace "," but only visually

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

in a RichTextLabel the user can enter a “,”. This comma splits things into left and right, therefore it’s crucial. Things break down if I replace the “,” with a “.” like bbcode_text = bbcode_text.replace(",","."), so I need to keep that “,”.

However, I’d like to let the user choose between “,” and “.” to be displayed. Is there any way to replace the “,” only visually while keeping it real in the background, only invisible? Maybe displaying the “.” simultaneously while offsetting the “,” far out of bounds? Or is there a general way to do this which I’m not aware of?

Wait, one more time. How is “,” crucial ? What is it for ?
Because I am pretty sure it is better to make a code requirring “,” to be more elastic, than to try hiding “,” in plain sight or placeholding it :slight_smile:

Inces | 2022-03-24 21:50

It’s a number-pad entry-field where you can place a comma so you have numbers in front and behind it. The code then deals with them in ways like var leftandrightofcomma = text.split(",", true, 1), pre-comma-digits and post-comma-digits etc.
Simply replacing the “,” with a “.” breaks all these functions, so I wonder if there’s a way to not really change anything but only “pretend” to… I guess I’m looking for some kind of neat hack here.
You certainly are right though, my code isn’t too flexible on that note for sure (I’m quite the noob yet).

pferft | 2022-03-24 22:56

Are the , and/or . supposed to be typed by the User, or are they inserted automatically based on the current value?

Regarding the actual problem… Why not define a variable that contains your “separator character” - either , or ..

Then, use that variable in (at least) the following locations:

  • In the code that inserts the separator into numeric string (assuming that happens automaticaly)
  • In the code that parses the string into its various components.

So, rather than this code (shown above):

var leftandrightofcomma = text.split(",", true, 1)

You’d do something like this:

var sep_char = '.' # or ',' (or, anything really)
.
.
.
var leftandrightofcomma = text.split(sep_char, true, 1)

jgodfrey | 2022-03-25 15:21

I should have thought of that early on, thanks for the suggestion.

The , and/or . are being inserted automatically.
The user can choose between , and . via a toggle-button, so i could arrange everything to consider if . chosen or if , chosen. But is there a way to have them both set into the same var sep_char = ' . *and/or ,* '?

pferft | 2022-03-28 18:40

But is there a way to have them both set into the same var sep_char = ’ . and/or , '?

Not sure I follow. You say that the User can set the , or . value via a toggle-button. If that’s the case, you’d want that user-set value instead of my above, hardcoded sep_char example. So, get the value from your User UI control (not sure what a “toggle-button” is - maybe that’s a CheckButton?). In that case, you could do something like:

var sep_char = ',' if $CheckButton.pressed else '.'

… or whatever is the correct logic based on your UI control setup. Then, use that value in your split code as noted above.

jgodfrey | 2022-03-28 19:09

var sep_char = ',' if $CheckButton.pressed else '.'

This is it. Now both , and . are being recognized and serve their purpose. Thank you!

pferft | 2022-03-28 21:10

:bust_in_silhouette: Reply From: jgodfrey

Converting to an “answer” for visibility on the main page.

Essentially, the solution is to

  • Determine which character separator (, or .) is desired by the User via a CheckButton UI control
  • Use that character in the code that separates the RichTextBox content for additional processing.

See above comments for additional details.