4

Lets consider I have a string called

string s = "jpeg, jpg, gif, png";

So from this I need to get each one like, I can assign each extension to one var variable such as

var a = jpeg
var b = jpg
var c = gif
var d = png

Same way if I will add more extensions to the string then accordingly I will have to get all with same var variable.

1
  • You copy/pasted the same stuff between all the answers, next time try to build a better question. Remember that this could help others that have the same problem. Commented Nov 19, 2015 at 11:51

8 Answers 8

16

All you have to do is to use the javascript split method-

$(function(){
    var s = "jpeg, jpg, gif, png";
    var match = s.split(', ')
    console.log(match)
    for (var a in match)
    {
        var variable = match[a]
        console.log(variable)
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
First : why is the allowedExtensions a string. It'd be much more nice to have it directly written as a javascript array. If you use jQuery, you can have the jQuery.inArray(ext, s.split(', ')) And if you don't have jQuery, you should find several inArray function thanks to Google, like this one
I've just said it, I think... I'm sorry not to be clear enough. I've updated the jsFiddle with more explanations
1

try Split function of javascript will do your task

string s = "jpeg, jpg, gif, png"; 
var arraylist = s.Split(", ") ;

point to Note : this provide you list of string i.e array of split string.......

1 Comment

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
1

You can get an array with the split method:

"jpeg, jpg, gif, png".split(', ');

You can then loop over it to get the individual values.

Assigning the values in it to individual variables is, frankly, insane, but you could play with eval to achieve that.

3 Comments

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
Why not ask a new question instead of adding a completely new requirement to the existing one?
where art thou semicolons?
0

s.Split(", ") should work.

1 Comment

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
0

just do:

var arr = s.split(", ");

then you have an array of the values, iterate over that and do what ever

1 Comment

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
0

Use split:

var list = s.split(', ');

1 Comment

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
0

lets consider you have a string like

**var** = s = "jpeg, jpg, gif, png";

split it with ',' and you have an array

var strArray = s.split(',')​

1 Comment

Thanks for your response. But actually I want to achieve a target as explained below. function onUploadClick1(){ var eachFileSize = '<%=EachFileSize %>'; if (fileUpload.value.length > 0) { var ext = getExt(filename); var s = '<%=AllowedExtensions %>'; var match = s.split(', ') for (var ext in match){ onUploadClick(); } } } Here I am using this function. So what I need is if the selected files extension will match with supplied extensions then it will allow or else dont allow.
0

If you have a comma delimited string, which was the question, there's no need to split.

In this case response is a json object returned from a service via an ajax call. The json contains an element called tags which is itself a collection object which I put in a javaScript array.

var metaTags = new Array();
metaTags = response.Tags;

for (var tag in metaTags) {
  $("#divTagContainer").append("<div class='divTagItem'>" + metaTags[tag] + "</div>");
};

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.