Questions tagged [trim]

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

2262 questions
2200
votes
26 answers

JavaScript chop/slice/trim off last character in string

I have a string, 12345.00, and I would like it to return 12345.0. I have looked at trim, but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions?
Phill Pafford
  • 77,927
  • 86
  • 256
  • 378
1333
votes
20 answers

Trim string in JavaScript?

How do I trim a string in JavaScript? That is, how do I remove all whitespace from the beginning and the end of the string in JavaScript?
Vinod
  • 29,295
  • 31
  • 91
  • 117
1254
votes
12 answers

How do I trim whitespace from a string?

How do I remove leading and trailing whitespace from a string in Python? For example: " Hello " --> "Hello" " Hello" --> "Hello" "Hello " --> "Hello" "Bob has a cat" --> "Bob has a cat"
robert
1135
votes
15 answers

How do I trim whitespace?

Is there a Python function that will trim whitespace (spaces and tabs) from a string? Example: \t example string\t → example string
Chris
  • 20,161
  • 24
  • 68
  • 94
1057
votes
45 answers

How to trim whitespace from a Bash variable?

I have a shell script with this code: var=`hg st -R "$path"` if [ -n "$var" ]; then echo $var fi But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace…
too much php
  • 81,874
  • 33
  • 123
  • 133
934
votes
11 answers

Remove all whitespace in a string

I want to eliminate all the whitespace from a string, on both ends, and in between words. I have this Python code: def my_handle(self): sentence = ' hello apple ' sentence.strip() But that only eliminates the whitespace on both sides of…
co2f2e
  • 15,562
  • 16
  • 60
  • 109
876
votes
48 answers

What's the best way to trim std::string?

I'm currently using the following code to right-trim all the std::strings in my programs: std::string s; s.erase(s.find_last_not_of(" \n\r\t")+1); It works fine, but I wonder if there are some end-cases where it might fail? Of course, answers with…
Milan Babuškov
  • 55,232
  • 47
  • 119
  • 176
466
votes
14 answers

.trim() in JavaScript not working in IE

I tried to apply .trim() to a string in one of my JavaScript programs. It's working fine under Mozilla, but an error displays when I try it in IE8. Does anyone know what is going on here? Is there anyway I can make it work in IE? code: var ID =…
Jin Yong
  • 38,582
  • 71
  • 132
  • 177
410
votes
16 answers

Does swift have a trim method on String?

Does swift have a trim method on String? For example: let result = " abc ".trim() // result == "abc"
tounaobun
  • 13,445
  • 9
  • 48
  • 75
380
votes
15 answers

How can I trim leading and trailing white space?

I am having some troubles with leading and trailing white space in a data.frame. For example, I like to take a look at a specific row in a data.frame based on a certain condition: > myDummy[myDummy$country == c("Austria"),c(1,2,3:7,19)] [1]…
mropa
  • 10,364
  • 9
  • 31
  • 29
370
votes
14 answers

Trim spaces from end of a NSString

I need to remove spaces from the end of a string. How can I do that? Example: if string is "Hello " it must become "Hello"
Andrea Mario Lufino
  • 7,496
  • 11
  • 41
  • 73
270
votes
10 answers

How to remove the leading character from a string?

I have a input string like: $str = ':this is a applepie :) '; How can I remove the first occurring : with PHP? Desired output: this is a applepie :)
yuli chika
  • 8,479
  • 19
  • 73
  • 121
255
votes
23 answers

Remove all spaces from a string in SQL Server

What is the best way to remove all spaces from a string in SQL Server 2008? LTRIM(RTRIM(' a b ')) would remove all spaces at the right and left of the string, but I also need to remove the space in the middle.
Ananth
  • 9,360
  • 22
  • 79
  • 108
231
votes
4 answers

Fastest way to remove first char in a String

Say we have the following string string data= "/temp string"; If we want to remove the first character / we can do by a lot of ways such as : data.Remove(0,1); data.TrimStart('/'); data.Substring(1); But, really I don't know which one has the…
Amr Badawy
  • 6,805
  • 12
  • 46
  • 81
227
votes
7 answers

How to remove all white space from the beginning or end of a string?

How can I remove all white space from the beginning and end of a string? Like so: "hello" returns "hello" "hello " returns "hello" " hello " returns "hello" " hello world " returns "hello world"
pedram
  • 3,265
  • 6
  • 21
  • 27
1
2 3
99 100