-2

Possible Duplicate:
How do I split this string with JavaScript?

In my rails app i'm concatenating strings and sending it to javascript via ajax technology. But for encreasing speed i think about one method... So simple i need to slice my string to three variables: My string look's so:

5.21o-o11.0o-o4

So how to slice it to

5.21

11.0

4

?

Community
  • 1
  • 1
byCoder
  • 3,462
  • 6
  • 28
  • 49
  • 5 correct answers for 1 minute! – VisioN Nov 14 '12 at 10:35
  • @Ben becouse some people here are "flying to high" and on questions of nubs simple - downwote( This is not good, becouse in google top i didn't find good solution for me... so opened question here... – byCoder Nov 14 '12 at 10:37
  • I didnt know js... so opened question here – byCoder Nov 14 '12 at 10:38
  • @undefined beatifull: there is +41, but i get minus one after another – byCoder Nov 14 '12 at 10:39
  • 1
    @PavelBY That's understandable. It's not the best attitude to duplicate the questions. – VisioN Nov 14 '12 at 10:40
  • @undefined this is discrimination! becouse i didn't know good english, and on query slice jquery string i didn't get any good result in google! just was good to write split – byCoder Nov 14 '12 at 10:40
  • That question had been asked in 2008, maybe the first one about this issue. – Ram Nov 14 '12 at 10:41
  • @VisioN it is not understandable for me, who didn't know js! Also good practice to set - (( i get frozen my acc, thank's a lot! – byCoder Nov 14 '12 at 10:41
  • 1
    @PavelBY it's OK to ask a duplicated question but if you **Google** the **title of your question** you'll find the answer in the **first page** of results... – Adriano Repetti Nov 14 '12 at 10:46
  • @Adriano i google'd slice! not split! and with slice google didnt get good results! That's the main problem! – byCoder Nov 14 '12 at 11:07
  • @PavelBY I mean even googling "slice" you get the result in the first page (even if not at the very top of the page). Anyway...why to worry? SO's people are clever than Google to understand what you want! :) – Adriano Repetti Nov 14 '12 at 11:11

6 Answers6

2

You can use split method:

var arr = '5.21o-o11.0o-o4'.split('o-o') // [ "5.21" , "11.0", "4" ]
Ram
  • 143,282
  • 16
  • 168
  • 197
2

Assuming o-o is the delimiter, use split() to get it into an array:

var foo = '5.21o-o11.0o-o4';
var values = foo.split('o-o');
alert(values[0]); // = 5.21 
alert(values[1]); // = 11.0
alert(values[2]); // = 4

This is obviously an example. Normally to get the values out you would iterate over the array as required.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

using the .split() method as so:

var text = "5.21o-o11.0o-o4​​";
var split = text.split("o-o");
alert(split[0] + ', ' + split[1] + ', ' + split[2]);

here's a fiddle

Pete Uh
  • 630
  • 4
  • 7
0

Try this code using split return the array with value

var str = '5.21o-o11.0o-o4';
var substr = str.split('o-o');

//substr[0] 5.21
//substr[1] 11
//substr[2] 4
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

Just add a .split('o-o') to the string

alert('5.21o-o11.0o-o4​​​'.split('o-o'));​​
Varon
  • 3,736
  • 21
  • 21
0

something like this?

var data ="5.21o-o11.0o-o4"; 
var spliced = data.split('o-o');
Tiit
  • 520
  • 4
  • 15