Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove whitespace near a character using regex in a long text
    text
    copied!<p>How do I delete one or mores white spaces near a character in a long text. I do not want to remove other white spaces which are not present adjacent to the matching string. I only want to remove all white spaces next to the matching character and not all white spaces of the input string. For example:</p> <pre><code>[text][space][space]![space][text] should result in [text]![text] [text][space][space]![space][space][space][text] should result in [text]![text] [text][space]![space][space][text] should result in [text]![text] [text][space]![space][text] should result in [text]![text] [text]![space][space][text] should result in [text]![text] [text][space][space]![text] should result in [text]![text] [text][space][space]! should result in [text]! ![space][space][text] should result in ![text] </code></pre> <p>The code I am going to write is:</p> <pre><code>for (int i = 0 to length of string) { if (string[i] == character) //which is the desired character "!" { int location = i+1; //remove all whitespace after the character till a non-whitespace character //is found or string ends while (string[location] == whitespace) { string[location].replace(" ", ""); location++; } int location = i-1; //remove all whitespace before the character till a non-whitespace character //is found or string ends while (string[location] == whitespace) { string[location].replace(" ", ""); location--; } } } </code></pre> <p>Is there a better way of removing whitespaces near a character using Regex?</p> <p>UPDATE: I do not want to remove other white spaces which are not present adjacent to the matching string. For example:</p> <pre><code>some_text[space]some_other_text[space][space]![space]some_text[space]some_other_text is some_text[space]some_other_text!some_text[space]some_other_text </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload