Error in adding commas to string [Resolved]

Posted by Klbaiju under jQuery on 2/18/2016 | Points: 10 | Views : 647 | Status : [Member] | Replies : 3
Hi,

I want to add commas to a string.

ie var strname="YYYYNN"

i need strname should be "Y,Y,Y,Y,N,N"

how it is possible.


Regards

Baiju




Responses

Posted by: A2H on: 2/18/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can try with the below code
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//Your string
var strname = "YYYYNN";
//variable to hold the result
var result = "";
//Loop through each items
for (var i = 0; i < strname.length; i++) {
result = result + strname.charAt(i) + ",";
}
//output the result
alert(result.slice(0, -1));
});
</script>


Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 2/18/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

alert("YYYYNN".split(''));


The algorithmic complexity will always be O(1).

hope this helps

--
Thanks & Regards,
RNA Team

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jitendrasoft09 on: 2/18/2016 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi,

Below is a working code-

string strname = "YYYYNN";
string name = string.Empty;
char[] thechars = strname.ToCharArray();
for (int i = 0; i <= thechars.Length - 1; i++)
{
name = name + thechars[i] + ",";

}
Console.Write(name.TrimEnd(','));


Jitendra Kumar
If my post helps you, plz mark as an answer.

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response