0

I am using the below string and I want to return the substring with the blankspaces in javascript.

How can I do this?

As of now I used the normal substring function but its giving me the result after removing the blankspaces.

var Sting="CREC                     20140615001CREC_GLOSS                                               18_0000033122                    GLO4265      SGLB201406152014090120140531 TESTFOREF       0000000000033122                8-1         EQTY      GB                            21419     ACTUALS   EUR000000000462098830          8738           N                                                                                                                                                                             C70390000501F              SQTY BUY 212102 49500.00@ 9.34                                                                        8738        8738                             "

var x= Sting.substring(1,27)

Result x= CREC 20

I don't want it like this, rather, I want my result to be

x= CREC                       20
Andy
  • 8,432
  • 6
  • 38
  • 76
Mrinal kumar
  • 67
  • 1
  • 1
  • 9
  • 4
    isn't the expected result exactly what you have? – SaintLike Apr 27 '15 at 13:32
  • I think what he wants is only one space instead of a lot of spaces between the words/ids codes, whatever. In hindsight, SO's comment editing feature is dysfunctional. – shash7 Apr 27 '15 at 13:33
  • 1
    @SaintLike - Seeing as how the OP is new, I'm guessing he didn't format the code/result exactly as he would have liked to in the question. – j08691 Apr 27 '15 at 13:33
  • 1
    You need to substring from 0 to 27, right now it loses the C. – Scimonster Apr 27 '15 at 13:35
  • It does seem to return all the spaces for me – adeneo Apr 27 '15 at 13:37
  • The spaces are there - I'm sure he is displaying them in a browser which shows them as a single space – Dobromir Velev Apr 27 '15 at 13:38
  • I don't see how you're getting the first `C` in your result when you use `1` with substring which would start with `R`. – j08691 Apr 27 '15 at 13:40
  • So the behavior of substring is like that only. Can you elaborate more to us what is your case. regex can be the answer for you question. – Kishor Pawar Apr 27 '15 at 13:41
  • Passing 1 as the first arg to substring will exclude the first character, which has the index 0. – Peter Herdenborg Apr 27 '15 at 13:41
  • Hey Guys if you see the result that i have got has only 1 space between CREC and 20 i want it tobe displayed in a format where it comes with ~20 spaces in between. i am getting the values but not in the correct format.sorry i used a 0 not 1 string.substing(0,27) – Mrinal kumar Apr 27 '15 at 13:45
  • You need to state clearly that this is not an issue with `substring()` removing the spaces, but that the spaces aren't shown when you display the string on a web page. If you skipped the substring bit you would still have the exact same problem. – Peter Herdenborg Apr 27 '15 at 13:49

3 Answers3

3

This is a browser thing - consecutive spaces are displayed as one. You might want to replace ' ' with   and then it will display all spaces indiviudaly

x.replace(" ", " "); 

Another way is to avoid displaying the string as HTML. For example the alert() function will show the correct number of spaces without the need to replace. Also displaying the string within a <pre> tag should work fine too.

Dobromir Velev
  • 530
  • 2
  • 15
2

I think Dobromir's interpretation of the OP is probably correct, and therefore a good answer.

If it is indeed a browser thing, then perhaps a better way to get it to display properly without changing the string itself is to style the HTML element with

white-space: pre

This will preserve the spaces like it does in a <pre> block.

See http://www.w3schools.com/cssref/pr_text_white-space.asp

randomsock
  • 955
  • 6
  • 9
1

If your spaces are always the same length you could do:

x.replace("                     ", " ");

otherwise you can use a regular expression to replace multiple spaces with one:

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

as seen Here

Community
  • 1
  • 1
Austinh100
  • 598
  • 3
  • 13