9

I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.

Thanks.

5 Answers 5

22
var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
Sign up to request clarification or add additional context in comments.

Comments

2
var str = 'firstHalf_0_0_0',
    part = str.match(/(\w+)Half/)[1];

alert(part); // Alerts "first"

Comments

2
var str = "firstHalf.....";
var index = str.indexOf("Half");
var substring = str.substr(0, index);

jsFiddle demo.

Comments

2

Using this you can get any particular part of string.

var str= 'your string';
var result = str.split('_')[0];

Comments

1

Working example here for your particular case.

http://jsfiddle.net/7kypu/3/

cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.