3

I'm using AJAX to retrieve data from MYSQL database through PHP.

However, if there is no result found, the variable still has two spaces. I found the problem using alert(data.length);. The result is 2, which means there are two spaces.

How can I remove these spaces so that if there is no result, I could display a message using if(data == ''){}?

Thank you!

chuff
  • 5,846
  • 1
  • 21
  • 26
Naveen Gamage
  • 1,844
  • 12
  • 32
  • 51
  • possible duplicate of [Remove characters from a string](http://stackoverflow.com/questions/4846978/remove-characters-from-a-string) and [How remove blank characters from a string in javascript?](http://stackoverflow.com/questions/3893625/how-remove-blank-characters-from-a-string-in-javascript). – Felix Kling Nov 28 '12 at 20:29
  • If I were you, I would [`trim`](http://php.net/trim) the response on the PHP end. – bfavaretto Nov 28 '12 at 20:33
  • @bfavaretto - I tried to do so, but I don't know, query passes through if($query) even query is empty – Naveen Gamage Nov 28 '12 at 20:42

7 Answers7

9

This is called string trimming. And here is one option for that in pure JavaScript:

var len = data.replace(/\s/g, "").length;

However, in modern browsers there is a string trim() function for that:

var len = data.trim().length;
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • but I don't want to remove spaces if there are some results, then spaces in those results will be also removed..I can try, if data.length == 2 then i can trim it.. thank you for ur answer... – Naveen Gamage Nov 28 '12 at 20:36
3

I can't understand why you have those two empty spaces in the first place. If it's a suitable option for you, I would try to remove those spaces at the origin, so from the server response.

If that's not possible you could use String.prototype.trim to remove leading/trailing white space. This would allow you to write your check as below

if (data.trim().length === 0) {
  ...
}
Bruno
  • 5,961
  • 6
  • 33
  • 52
1
var str = "lots of whitespace";
str.replace(/\s+/g, ''); // 'lotsofwhitespace'
lostsource
  • 21,070
  • 8
  • 66
  • 88
1

See this post where the topic is covered deeply, if you use jQuery, you can use $.trim() which is built-in.

Community
  • 1
  • 1
David Müller
  • 5,291
  • 2
  • 29
  • 33
1

if your variable is data then try this

    data.replace(/\s+/g, '');

and assign it to other variable if you want

data2 = data.replace(/\s+/g, '');
echo_Me
  • 37,078
  • 5
  • 58
  • 78
1

3 options:

var strg = "there is space";
strg.replace(/\s+/g, ' ');

or:

$.trim()

or:

String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/g, ""); 
}

var strg = "there is space";
strg.trim();
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • but I don't want to remove spaces if there are any results, then spaces in those results will be also removed.. now I can do if data.length == 2 then trim it using ur way.. thank you very much.. – Naveen Gamage Nov 28 '12 at 20:38
1

data = data.split(' ').join('') that will remove all spaces from a string.

AJ Genung
  • 341
  • 1
  • 8