How to break a string after a specific character in jquery

Here is my code:

var string1= "Hello how are =you"; 

I want a line after "=", that is, "you" only from this entire line. Suppose a string will always have a single "=" character, and I want the entire string after that character in a new variable in jquery.

Please help me.

-one
javascript
Jun 11. '14 at 6:59
source share
4 answers

Demo script

Use this: jQuery split () ,

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

Split gives you two outputs:

  • [0] = "Hello, how to do this"

  • [1] = "you"

+5
Jun 11 '14 at 7:02
source share

use the Split method to split a string into an array

demonstration

 var string1= "Hello how are =you"; alert(string1.split("=")[1]); 
+3
Jun 11 '14 at 7:01
source share

Try using String.prototype.substring() in this context,

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

Demo

Proof of speed when comparing with other answers using .split()

+2
Jun 11. '14 at 7:00
source share

Use .split() in javascript

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

Demo

0
Jun 11 '14 at 7:00
source share



All Articles