-5

I've built a search index in Cloudant. I use trim() to remove space in string. However, it does not work.

How can I do this?

Update:

I have a JSON object

...
    "attributeArray": [
      {
        "name": "this is  a       web     authentication"
      }
    }
...

I already extracted "name" successfully. I want to remove space in "name" then make a search index for the document. Supposing "name" has already been extracted.

var index=name.trim();
Index("default", index);

When I query, the system shows:

{
"id": "06xxxx",
"fields": [
" this is  a       web     authentication"
]
}

I conclude that the function trim() does not work.

PS: A small question so it does not need to explain in whole thing.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Hong
  • 25
  • 6
  • two negative points but no-one tried with that. – Hong May 04 '16 at 18:20
  • 1
    What do you mean by "it does not work"? Btw: It's 3 "negative points" now. – Derek 朕會功夫 May 04 '16 at 18:20
  • 1
    I'd suggest you read the [doc for `.trim()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) and then show us an exact example of where it isn't doing what the doc says it does. The downvotes are probably because you just aren't reading the documentation for what `.trim()` does and are now claiming it does not work without even describing your test case. This is a poorly written question for stack overflow. Since you may be new here, please read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask). – jfriend00 May 04 '16 at 18:20
  • Probably also worth reading: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to help you understand how to post good questions here that won't get downvotes and will get good answers. Your question will probably get closed because you haven't shown the code you are using so we can't see what you are doing wrong. `.trim()` works just fine. The problem is with your code and how you are using it and you have not disclosed that at all so nothing we can do but guess. – jfriend00 May 04 '16 at 18:24
  • #Derek it does not with the goal of this function as in fure JS, Java. Yeah, I know you always have a negative mind. – Hong May 04 '16 at 18:24
  • @Hong: Try including your code so we can see what you're trying to do. – IMTheNachoMan May 04 '16 at 18:25
  • #jfiend00 thanks, I coded many time with JS and Java. The best way i just want to use trim() (no need with other action). Cloudant supports JS, but a lot of JS functions do not work there. – Hong May 04 '16 at 18:26
  • Show us the ACTUAL CODE you are using, the result you expected and the result you got and a good answer would have been posted within minutes of your posting with no downvotes. That's how this site works best. FYI, this site does not use the `#` tag like you are trying to us it. If you want to notify a given user, you use `@Hong`. – jfriend00 May 04 '16 at 18:27
  • #jfrined00 i think my question is enough information. Do you need if I show Json document, search index code and the output? – Hong May 04 '16 at 18:30
  • $ or # is not a problem guy, sometime when i use $ it does not appear. – Hong May 04 '16 at 18:31
  • @Hong: *"i think my question is enough information"* No it's not. "It does not work" is not useful information. We already know that *something* doesn't work, otherwise you wouldn't be posting here. You have to be more specific: Are you not getting the result that you expect? If yes, what's the input, the current output and the expected output? Or are you getting an error, such as a syntax error or a runtime error? There are infinite ways how a piece of code could "not work". We (and you) don't have time to explore all of them. – Felix Kling May 04 '16 at 18:32
  • Or put differently: Lets say someone posts the question *"I'm trying to use `arr.push()` but it doesn't work, please help!"*, would you be able to provide a clear answer? – Felix Kling May 04 '16 at 18:35
  • To you give you a couple of examples what the issue could be: (1) You are simply using `.trim()` incorrectly because you are not aware that it returns a new string. (2) You have a wrong impression of what `.trim()` does, so it's not the right function to use. (3) You have a syntax error somewhere. (4) The environment the code runs in doesn't support `.trim()`. It's likely one (or more) of these four issues. Are you able to fix your code knowing this? – Felix Kling May 04 '16 at 18:40
  • @Felix guy, no need explain many long sentences like that. it does not make sense. – Hong May 04 '16 at 18:49
  • There is no need for long comments if you provide all the information we need. From your edit, it seems the issue was #2: *"You have a wrong impression of what `.trim()` does, so it's not the right function to use."* How can you expect that we know that you don't know how `.trim()` works? Anyways, I hope you have understood that your original question didn't contain sufficient information. – Felix Kling May 04 '16 at 19:09
  • @Felix I am tired with you in a very ...very small question. Please stop here and be more easier with other questions. – Hong May 04 '16 at 19:14

3 Answers3

3

By definition, the trim() function will only remove leading and trailing white-space within a string, which may not be what you need in this scenario.

If you want to remove all white-space in general, you could consider using a Regular Expression replacement via the replace() function:

// This will remove all white-space from your input string
var output = input.replace(/\s/g,'');

After Your Update

Your code looks a bit more like you want to replace multiple instances of a space with a single space, which can still be done by a slightly different expression than the original :

// This replaces multiple repeated instances of a space with a single
var trimmedName = name.replace(/\s+/g,' ');
Index("default", trimmedName);
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • Do you have an example of some of your input and what you expect to be returned as output? – Rion Williams May 04 '16 at 18:28
  • As mentioned `trim()` will remove only the leading and trailing white space within the string (that is spaces that appear at the beginning or the end of the string). You'll likely need to use something like the `replace()` function as I mentioned in my post to handle this. If you were to use " this is a test", trimming it would return "this is a test", whereas the regular expression approach would turn it into "this is a test". – Rion Williams May 04 '16 at 18:51
  • Wonderful explanation. – Hong May 04 '16 at 18:59
1

trim() function will only remove leading and trailing white-space of a string that will ot remove spaces between string words

Thorin
  • 2,004
  • 1
  • 19
  • 30
1

Are you making sure that you're re-assigning your variable after the trim()?

var test = "   Hello World   ";

test = test.trim(); // "Hello World"
Jamie
  • 467
  • 8
  • 19