Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string after a particular character in jquery [duplicate]

Tags:

javascript

Here is my code :

var string1= "Hello how are =you";

I want a string after "=" i.e. "you" only from this whole string. Suppose the string will always have one "=" character and i want all the string after that character in a new variable in jquery.

Please help me out.

like image 214
deepak.mr888 Avatar asked Jun 11 '14 06:06

deepak.mr888


4 Answers

Demo Fiddle

Use this : jQuery split(),

var string1= "Hello how are =you";
string1 = string1.split('=')[1];

Split gives you two outputs:

  • [0] = "Hello how are "

  • [1] = "you"

like image 130
Shaunak D Avatar answered Oct 06 '22 04:10

Shaunak D


Try to use String.prototype.substring() in this context,

var string1= "Hello how are =you"; 
var result = string1.substring(string1.indexOf('=') + 1);

DEMO

Proof for the Speed in execution while comparing with other answers which uses .split()

like image 35
Rajaprabhu Aravindasamy Avatar answered Oct 06 '22 03:10

Rajaprabhu Aravindasamy


use Split method to split the string into array

demo

var string1= "Hello how are =you";

alert(string1.split("=")[1]);
like image 38
Balachandran Avatar answered Oct 06 '22 04:10

Balachandran


Use .split() in javascript

var string1= "Hello how are =you";

console.log(string1.split("=")[1]); // returns "you"

Demo

like image 34
Sudharsan S Avatar answered Oct 06 '22 02:10

Sudharsan S