Manipulating strings with jQuery

Hello Experts,

I have a string e.g. "0006".
I need to split it and add an underscore between the first two digits and the last two digits e.g. "00_06".
What would be the easiest  way to do it using jQuery?

Thanks!
RefaelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
JQuery is designed for DOM manipulation. To do string manipulation, just use javascript.
var str = "0006";
str = str.substr(0, 2) + "_" + str.substr(2, str.length);

Open in new window

RefaelAuthor Commented:
Hi Tom.
It does not seem to work.... Here is my code with comments...

	var theId = $(this).closest(".img-container").attr("id");
	var theidSubs = theId.substring(7);     // here i take the first 7 characters out
	var theURL  = theidSubs.substr(0, 2) + "_" + theidSubs.substr(2, theidSubs.length); // this is your code
	$(this).attr("href", "http://domain.com/we_"+theURL+".pdf");  // the final URL

Open in new window

Tom BeckCommented:
My test of your code:
var theId = "07seven0006";
	var theidSubs = theId.substring(7);     // here i take the first 7 characters out
	var theURL  = theidSubs.substr(0, 2) + "_" + theidSubs.substr(2, theidSubs.length); // this is your code
	//$(this).attr("href", "http://domain.com/we_"+theURL+".pdf");  // the final URL
	alert("http://domain.com/we_"+theURL+".pdf");

Open in new window

Works as expected.
00_06

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RefaelAuthor Commented:
Tom Thank You!
I tested it again and it works prefect!
Tom BeckCommented:
Good deal. Thanks for the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.