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
2505
votes
25 answers

How do I chop/slice/trim off last character in string using Javascript?

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
  • 83,471
  • 91
  • 263
  • 383
1363
votes
20 answers

Trim string in JavaScript

How do I remove all whitespace from the start and end of the string?
Vinod
  • 31,933
  • 35
  • 96
  • 119
1355
votes
13 answers

How do I trim whitespace from a string?

How do I remove leading and trailing whitespace from a string in Python? " Hello world " --> "Hello world" " Hello world" --> "Hello world" "Hello world " --> "Hello world" "Hello world" --> "Hello world"
robert
1309
votes
52 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
  • 88,666
  • 34
  • 128
  • 138
1206
votes
15 answers

How do I trim whitespace?

Is there a Python function that will trim whitespace (spaces and tabs) from a string? So that given input " \t example string\t " becomes "example string".
Chris
  • 21,549
  • 25
  • 71
  • 99
1198
votes
15 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…
not 0x12
  • 19,360
  • 22
  • 67
  • 133
996
votes
51 answers

How to trim an 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
  • 59,775
  • 49
  • 126
  • 179
470
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
  • 42,698
  • 72
  • 141
  • 187
424
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
  • 14,570
  • 9
  • 53
  • 75
414
votes
15 answers

How can I trim leading and trailing white space?

I am having some trouble with leading and trailing white space in a data.frame. For example, I 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] codeHelper …
mropa
  • 11,562
  • 10
  • 33
  • 29
372
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,921
  • 12
  • 47
  • 78
312
votes
12 answers

How to trim a string to N chars in Javascript?

How can I, using Javascript, make a function that will trim string passed as argument, to a specified length, also passed as argument. For example: var string = "this is a string"; var length = 6; var trimmedString = trimFunction(length,…
user825286
304
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
  • 9,053
  • 20
  • 75
  • 122
273
votes
5 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
  • 7,453
  • 12
  • 49
  • 84
257
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,647
  • 6
  • 24
  • 28
1
2 3
99 100