Remove Extra Spaces But Not Space Between Two Words

Remove extra spaces but not space between two words

$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $str)));

how to remove more than one space between two words of string in angular?

Use a regular expression to match two or more space characters, and replace with a single space:

const folderName = '    aaa         aaa';console.log(  folderName    .replace(/ {2,}/g, ' ')    .trim());

Removing all but one whitespace between words in C++

The problem is in the conditional

if (temp.back() == ' ' && c != ' ')

you want to add the space if last character is NOT a space and c is a space:

if (temp.back() != ' ' && c == ' ')

(you reversed == and != operators).

Also you need to push the new character c AFTER this conditional block (otherwise temp.back() and c will always be the same character).

Finally at the beginning the string temp is empty, calling back() is not allowed, you should initialize it with a non-blank instead (e.g. temp = "x").

The final function that works is therefore:

string removeWhiteSpace(string current)
{
string myNewString = "";
string temp = "x";
for (char c : current)
{
if (temp.back() != ' ' && c == ' ')
{
myNewString.push_back(' ');
}
temp.push_back(c);
if (c != ' ')
{
myNewString.push_back(c);
}
}
return myNewString;
}

How to remove extra white space between words inside a character vector using?

gsub is your friend:

test <- "Hi,  this is a   good  time to   start working   together."
gsub("\\s+"," ",test)
#[1] "Hi, this is a good time to start working together."

\\s+ will match any space character (space, tab etc), or repeats of space characters, and will replace it with a single space " ".

How to remove extra spaces between two words in a string?

As an alternative, split and rebuilt your string:

string input = "Hello world (707)(y)(9) ";
System.Text.RegularExpressions.Regex re = new
System.Text.RegularExpressions.Regex("[;\\\\/:*?\"<>|&'()-]");
//split
var x = re.Split(input);
//rebuild
var newString = string.Join(" ", x.Where(c => !string.IsNullOrWhiteSpace(c))
.Select(c => c.Trim()));

newString equals:

Hello world 707 y 9

Remove lines , But not space between two words in Notepad++

It seems all you need is to make sure a whitespace is also considered a valid char, add it to the lookahead in the first regex:

^(?![a-zA-Z,.'\h]+$).+$\R?

The \h is used instead of \s in order to prevent jumping to another line. \s matches line breaks, and \h only matches a horizontal whitespace.
Sample Image

remove two or more empty between space in word

you can use like below

string xyz = "1   2   3   4   5";
xyz = string.Join( "-", xyz.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ));

Reference

  1. How do I replace multiple spaces with a single space in C#?
  2. How to replace multiple white spaces with one white space

Remove the space between two words in PHP

'PaneerPakodaDish' should be the desired output.

$string = 'Paneer Pakoda dish';
$s = ucfirst($string);
$bar = ucwords(strtolower($s));
echo $data = preg_replace('/\s+/', '', $bar);

It will give you the exact output 'PaneerPakodaDish' where character "D" will also be in capital.

Remove the space between two words

The easiest way would just be to remove any spaces between ] and [, if that words for you.

$string = '[/TD] [TD="align: left"]';
$string = preg_replace('/\]\s+\[/', '][', $string);

Javascript - How to remove all extra spacing between words

var string = "    This    should  become   something          else   too . ";
string = string.replace(/\s+/g, " ");

This code replaces a consecutive set of whitespace characters (\s+) by a single white space. Note that a white-space character also includes tab and newlines. Replace \s by a space if you only want to replace spaces.

If you also want to remove the whitespace at the beginning and end, include:

string = string.replace(/^\s+|\s+$/g, "");

This line removes all white-space characters at the beginning (^) and end ($). The g at the end of the RegExp means: global, ie match and replace all occurences.



Related Topics



Leave a reply



Submit