Removing Leading and Trailing Spaces from a String

How to remove leading and trailing whitespace from the string in Java?

 s.trim()

see String#trim()

Without any internal method, use regex like

 s.replaceAll("^\\s+", "").replaceAll("\\s+$", "")

or

  s.replaceAll("^\\s+|\\s+$", "")

or just use pattern in pure form

    String s="          Hello World                    ";
Pattern trimmer = Pattern.compile("^\\s+|\\s+$");
Matcher m = trimmer.matcher(s);
StringBuffer out = new StringBuffer();
while(m.find())
m.appendReplacement(out, "");
m.appendTail(out);
System.out.println(out+"!");

How to remove leading and trailing spaces from a string

String.Trim

Removes all leading and trailing white-space characters from the current String object.

Usage:

txt = txt.Trim();

If this isn't working then it highly likely that the "spaces" aren't spaces but some other non printing or white space character, possibly tabs. In this case you need to use the String.Trim method which takes an array of characters:

  char[] charsToTrim = { ' ', '\t' };
string result = txt.Trim(charsToTrim);

Source

You can add to this list as and when you come across more space like characters that are in your input data. Storing this list of characters in your database or configuration file would also mean that you don't have to rebuild your application each time you come across a new character to check for.

NOTE

As of .NET 4 .Trim() removes any character that Char.IsWhiteSpace returns true for so it should work for most cases you come across. Given this, it's probably not a good idea to replace this call with the one that takes a list of characters you have to maintain.

It would be better to call the default .Trim() and then call the method with your list of characters.

How to remove leading and trailing spaces from a string?

You can use the strip() method to remove trailing and leading spaces:

>>> s = '   abd cde   '
>>> s.strip()
'abd cde'

Note: the internal spaces are preserved.

Removing leading and trailing spaces from a string

This is called trimming. If you can use Boost, I'd recommend it.

Otherwise, use find_first_not_of to get the index of the first non-whitespace character, then find_last_not_of to get the index from the end that isn't whitespace. With these, use substr to get the sub-string with no surrounding whitespace.

In response to your edit, I don't know the term but I'd guess something along the lines of "reduce", so that's what I called it. :) (Note, I've changed the white-space to be a parameter, for flexibility)

#include <iostream>
#include <string>

std::string trim(const std::string& str,
const std::string& whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content

const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;

return str.substr(strBegin, strRange);
}

std::string reduce(const std::string& str,
const std::string& fill = " ",
const std::string& whitespace = " \t")
{
// trim first
auto result = trim(str, whitespace);

// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;

result.replace(beginSpace, range, fill);

const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}

return result;
}

int main(void)
{
const std::string foo = " too much\t \tspace\t\t\t ";
const std::string bar = "one\ntwo";

std::cout << "[" << trim(foo) << "]" << std::endl;
std::cout << "[" << reduce(foo) << "]" << std::endl;
std::cout << "[" << reduce(foo, "-") << "]" << std::endl;

std::cout << "[" << trim(bar) << "]" << std::endl;
}

Result:

[too much               space]  
[too much space]
[too-much-space]
[one
two]

JavaScript remove leading and trailing spaces

try doing

trimmedstr = str.replace(/\s+$/, '');

or maybe

.replace(/ +$/, "");

Remove leading and trailing whitespace from list of string in C#

You can trim leading and trailing whitespace on each item by using a Linq selector:

var withoutWhitespace = aList.Select(item => item.Trim()).ToList();

How do I trim leading/trailing whitespace in a standard way?

If you can modify the string:

// Note: This function returns a pointer to a substring of the original string.
// If the given string was allocated dynamically, the caller must not overwrite
// that pointer with the returned value, since the original pointer must be
// deallocated using the same allocator with which it was allocated. The return
// value must NOT be deallocated using free() etc.
char *trimwhitespace(char *str)
{
char *end;

// Trim leading space
while(isspace((unsigned char)*str)) str++;

if(*str == 0) // All spaces?
return str;

// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;

// Write new null terminator character
end[1] = '\0';

return str;
}

If you can't modify the string, then you can use basically the same method:

// Stores the trimmed input string into the given output buffer, which must be
// large enough to store the result. If it is too small, the output is
// truncated.
size_t trimwhitespace(char *out, size_t len, const char *str)
{
if(len == 0)
return 0;

const char *end;
size_t out_size;

// Trim leading space
while(isspace((unsigned char)*str)) str++;

if(*str == 0) // All spaces?
{
*out = 0;
return 1;
}

// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
end++;

// Set output size to minimum of trimmed string length and buffer size minus 1
out_size = (end - str) < len-1 ? (end - str) : len-1;

// Copy trimmed string and add null terminator
memcpy(out, str, out_size);
out[out_size] = 0;

return out_size;
}

Remove every space EXCEPT leading spaces

You can use replaceAll with this regex (?<=\S)(\s+)(?=\S) like this :

str = str.replaceAll("(?<=\\S)(\\s+)(?=\\S)", "");

Examples of input & outputs:

"              h   ello  "        => "              hello  "
" hello, word " => " hello,word "

The first regex keep only leading and trailing spaces, if you want to keep only the leading spaces, then you can use this regex (?<=\S)(\s+).

Examples of input & outputs:

"              hello  "         => "              hello"
" hello, word " => " hello,word"

How to remove only trailing spaces of a string in Java and keep leading spaces?

Since JDK 11

If you are on JDK 11 or higher you should probably be using stripTrailing().


Earlier JDK versions

Using the regular expression \s++$, you can replace all trailing space characters (includes space and tab characters) with the empty string ("").

final String text = "  foo   ";
System.out.println(text.replaceFirst("\\s++$", ""));

Output

  foo

Online demo.

Here's a breakdown of the regex:

  • \s – any whitespace character,
  • ++ – match one or more of the previous token (possessively); i.e., match one or more whitespace character. The + pattern is used in its possessive form ++, which takes less time to detect the case when the pattern does not match.
  • $ – the end of the string.

Thus, the regular expression will match as much whitespace as it can that is followed directly by the end of the string: in other words, the trailing whitespace.

The investment into learning regular expressions will become more valuable, if you need to extend your requirements later on.

References

  • Java regular expression syntax


Related Topics



Leave a reply



Submit