Make A List Comma And "and"-separated
You have strings with a separator character (a space or something else) like the following: '1 2 3' '1 2' '1' What is the best/elegant way in Javascript to format them in the foll
Solution 1:
Got this somewhere on StackOverflow a few years ago. This is a very basic implemenation -- it wouldn't be hard to add a delimiter parameter to the method.
functionformatList(myString){
if ( myString.indexOf(" ") === -1 ) return myString;
if ( myString.indexOf(" ") === myString.lastIndexOf(" ") ){
return myString.replace(" ", " and ");
}
var former = myString.substr(0, myString.lastIndexOf(" "));
former = former.replace(/ /g,", ");
var latter = myString.substr(myString.lastIndexOf(" "), myString.length -1);
var output = former + " and " + latter;
return output;
}
formatList("1"); // 1formatList("1 2"); // 1 and 2formatList("1 2 3"); // 1, 2 and 3formatList("1 2 3 4"); // 1, 2, 3 and 4
Solution 2:
You can use this:
<!DOCTYPE html><html><body><script>
cars=["Elem 1","Elem 2","Elem 3","Elem 4"];
var s = '';
var sep = '';
for (var i=0;i<cars.length;i++)
{
if(i==cars.length-1){
s=s+" and "+cars[i];
}else{
s=s+sep+cars[i];
sep = ', ';
}
}
document.write(s + "<br>");
</script></body></html>
Solution 3:
This one works if you have an array too. It uses the Query $.inArray that can be omitted if you do not use jQuery and input only in array form or string form.
// Transform a string made of substrings separated by a separator (default "#")// or an array in a string that is comma (", ") separated and that has an "and" // separaing the last element. // Ex: // toCommaAnd("1#2#3") -> "1, 2 and 3"// toCommaAnd("1 2 3"," ") -> "1, 2 and 3"// toCommaAnd("Mr. Cinelli#Ms. Cinelli") ->"Mr. Cinelli and Ms. Cinelli"// toCommaAnd(["a", "b"]) -> "a and b"//// !! toCommaAnd("1#2#3#") -> become "1, 2 and 3" TOO so you can // concatenate elements in loops without // caring about special case for first or// last element. functiontoCommaAnd(str, separator) {
var ar = $.isArray(str) ? str : str.split(separator || "#"),
commaUntil = ar.length - 1,
ret = ar[0],
i;
//This is just for the 1#2#3# cases.if (commaUntil > 0 && ar[commaUntil] === ""){
commaUntil--;
}
for (i = 1; i < commaUntil; i++){
ret += ", " + ar[i];
}
if (commaUntil > 0)
ret += " and " + ar[commaUntil];
return ret ;
},
Post a Comment for "Make A List Comma And "and"-separated"