Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split the string by last comma

Tags:

jquery

string

I have string in jQuery.

var s = "Text1, Text2, Text,true";

I need split this variable in two part:

var s1 = substring(...); //after this s1="Text1, Text2, Text"
var s2 = substring(...); //after this s2="true"

Can you help me split variable?

like image 958
titans Avatar asked Aug 15 '13 08:08

titans


People also ask

How do you split a string with last comma in Python?

For example, comma(,). Python provides a method that split the string from rear end of the string. The inbuilt Python function rsplit() that split the string on the last occurrence of the delimiter. In rsplit() function 1 is passed with the argument so it breaks the string only taking one delimiter from last.

What is split () function in string?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do I split a string with a comma in R?

The splitting of comma separated values in an R vector can be done by unlisting the elements of the vector then using strsplit function for splitting. For example, if we have a vector say x that contains comma separated values then the splitting of those values will be done by using the command unlist(strsplit(x,",")).


2 Answers

var s="Text1, Text2, Text,true";
var lastIndex = s.lastIndexOf(",")

var s1 = s.substring(0, lastIndex); //after this s1="Text1, Text2, Text"
var s2 = s.substring(lastIndex + 1); //after this s2="true"
like image 117
Ionică Bizău Avatar answered Oct 31 '22 03:10

Ionică Bizău


s1 = s.split(',');
s2 = s1.pop();
s1 = s1.join(',');

(untestet)

Docs

  • http://www.w3schools.com/jsref/jsref_split.asp
  • http://www.w3schools.com/jsref/jsref_pop.asp
  • http://www.w3schools.com/jsref/jsref_join.asp
like image 31
BreyndotEchse Avatar answered Oct 31 '22 04:10

BreyndotEchse