0

I'm trying to check if a string has >= and then, if so, replace that with the ascii character for greater than and equal to. I'm working in Angular in the TS file and currently have:

@Input()
  public set textLabel(value: string) {
  let labelSymbols = value
  // figure out how to check if string has >=
  // if string has >=, replace with ASCII character
  this._textLabel = labelSymbols

  this._changeDetectorRef.detectChanges();
}
  public get textLabel(): string {
    return this._textLabel
  }
  private _textLabel: string;

What do I need to do to change the greater than and equal to when it occurs in the string?

BR Avery
  • 1
  • 1
  • 2
    Your question does not make sens. You are trying to check if string has >=, but greater or equals to what ? It's not clear what you are trying to compare. – Nicolas Oct 07 '21 at 21:58
  • Do you just mean a literal `>=`? In which case just check the substr index is not -1, like in JS – 2e0byo Oct 07 '21 at 22:02
  • I'm trying to check if the value contains the symbols ">=". I need to replace every instance of the symbols ">=" with ≥ – BR Avery Oct 07 '21 at 22:03
  • Does this answer your question? [How to replace all occurrences of a string in JavaScript](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Akxe Oct 07 '21 at 22:12
  • 1
    @BRAvery just so you know, ≥ isn't an ASCII character. It is a Unicode character however – ShamPooSham Oct 07 '21 at 22:16
  • Also, the `let labelSymbols = value` assignment feels really unnecessary. Just do the operations directly on `value`. Rename it to `labelSymbols` if you prefer – ShamPooSham Oct 07 '21 at 22:18

2 Answers2

1

From the info I got from your comment, you are simply looking for a search and replace. You can use the replaceAll function to do that.

function replaceAllGreaterOrEqualsChar(input) {
  return input.replaceAll('>=', '≥');
}

const originalString = "this is >= a test, with >= multiple instances.";

// we pass the original string in our custom function,
const output = replaceAllGreaterOrEqualsChar(originalString);

// we print the results to the console.
console.log('output', output);
Nicolas
  • 8,077
  • 4
  • 21
  • 51
1

You can use the regular expression, />=/g to find every occurrence of >= and replace it with , as shown in the below code snippet,

@Input()
public set textLabel(value: string) {
  let regexToMatch = />=/g
  this._textLabel = value.replace(regexToMatch, "≥");;

  this._changeDetectorRef.detectChanges();
}

public get textLabel(): string {
  return this._textLabel
}

private _textLabel: string;
thisdotutkarsh
  • 940
  • 6
  • 7