Questions tagged [trim]

Trimming refers to the manipulation of a text string to remove leading and/or trailing whitespace (and/or ASCII control characters), or (optionally) a specified character.

2454 questions
248
votes
7 answers

How do I remove leading whitespace in Python?

I have a text string that starts with a number of spaces, varying between 2 & 4. What is the simplest way to remove the leading whitespace? (ie. remove everything before a certain character?) " Example" -> "Example" " Example " -> "Example " "…
James Wanchai
  • 2,861
  • 4
  • 21
  • 16
218
votes
22 answers

Trim specific character from a string

What's the JavaScript equivalent to this C# Method: var x = "|f|oo||"; var y = x.Trim('|'); // "f|oo" C# trims the selected character only at the beginning and end of the string!
fubo
  • 44,811
  • 17
  • 103
  • 137
217
votes
11 answers

Explode string on commas and trim potential spaces from each value

For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] =…
SeanWM
  • 16,789
  • 7
  • 51
  • 83
211
votes
11 answers

Replace multiple whitespaces with single whitespace in JavaScript string

I have strings with extra whitespace characters. Each time there's more than one whitespace, I'd like it be only one. How can I do this using JavaScript?
eve
  • 2,959
  • 4
  • 21
  • 18
205
votes
8 answers

String strip() for JavaScript?

How do I strip leading and trailing spaces from a string? For example, " dog " should become "dog".
rawrrrrrrrr
  • 3,647
  • 7
  • 28
  • 33
204
votes
4 answers

Difference between String trim() and strip() methods in Java 11

Among other changes, JDK 11 introduces 6 new methods for java.lang.String class: repeat(int) - Repeats the String as many times as provided by the int parameter lines() - Uses a Spliterator to lazily provide lines from the source string isBlank() -…
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
203
votes
40 answers

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

Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I'd roll my own, but I would think this is a common problem with an equally common solution.
coledot
  • 2,646
  • 3
  • 19
  • 17
196
votes
2 answers

How can I trim all strings in an Array?

If I have this array: array(" hey ", "bla ", " test"); and I want to trim all of them, How can I do that? The array after the trim: array("hey", "bla", "test");
Daniel
  • 2,441
  • 4
  • 18
  • 12
184
votes
7 answers

Remove whitespaces inside a string in javascript

I've read this question about javascript trim, with a regex answer. Then I expect trim to remove the inner space between Hello and World. function myFunction() { alert("Hello World ".trim()); } EDITED Why I expected that!? Nonsense!…
Hernán Eche
  • 6,529
  • 12
  • 51
  • 76
182
votes
18 answers

How can I trim beginning and ending double quotes from a string?

I would like to trim a beginning and ending double quote (") from a string. How can I achieve that in Java? Thanks!
ufk
  • 30,912
  • 70
  • 235
  • 386
167
votes
10 answers

Replace all whitespace characters

I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript. How to do so? I tried: str.replace(/ /gi, "X")
Sourav
  • 17,065
  • 35
  • 101
  • 159
166
votes
9 answers

How can I split and trim a string into parts all on one line?

I want to split this line: string line = "First Name ; string ; firstName"; into an array of their trimmed versions: "First Name" "string" "firstName" How can I do this all on one line? The following gives me an error "cannot convert type…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
156
votes
10 answers

Strip / trim all strings of a dataframe

Cleaning the values of a multitype data frame in python/pandas, I want to trim the strings. I am currently doing it in two instructions : import pandas as pd df = pd.DataFrame([[' a ', 10], [' c ', 5]]) df.replace('^\s+', '', regex=True,…
mxdbld
  • 16,747
  • 5
  • 34
  • 37
129
votes
9 answers

Trim trailing spaces in Xcode

Is there a way to force Xcode to trim trailing whitespaces when I save file? I'm using version 3.1.3 if that matters.
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
117
votes
17 answers

How many spaces will Java String.trim() remove?

In Java, I have a String like this: " content ". Will String.trim() remove all spaces on these sides or just one space on each?
oneat
  • 10,778
  • 16
  • 52
  • 70