-1

Here is my PHP code..

$input = "zergling-light"
$output = str_replace('-', ' (', $input).")";
$output = strtoupper(substr($input, 0, 1)).substr($input, 1);
echo $output;
// Prints.. Zergling (light)

..I'm extremely crappy with my JavaScript code, could someone help me convert this? I basically want to convert a variable such as "marine-heavy" to "Marine (heavy)".

asked Oct 26, 2009 at 4:50

3 Answers 3

2

This should do what you want, assuming that all inputs are of the correct form (i.e. "part1-part2")

input = "marine-heavy";
parts = input.split("-");
output = parts[0] + " (" + parts[1] + ")";
output = output[0][0].toUpperCase() + output.substring(1);
answered Oct 26, 2009 at 4:56
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - For doing the heavy lifting of coming up with a simple solution that will get most of the way, in a portable way.
2

David has a good start, but on some versions of IE a string cannot be treated as an array. In javascript, can I override the brackets to access characters in a string?

var input = "marine-heavy";
var parts = input.split("-");
var output = parts[0] + " (" + parts[1] + ")";
output = output[0].charAt(0).toUpperCase() + output.substring(1);
answered Oct 26, 2009 at 5:08

Comments

1

Another approach might be:

var input = "zergling-light";
var output = input.charAt(0).toUpperCase() + input.substring(1).replace("-", " (") + ")";
answered Oct 26, 2009 at 5:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.