/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Looping through a string value

Hi, I’ve got a string that holds a content similar to this:

[COLOR=DarkOrange] var PreSelectedIDS = “7,8,10,55,250,12”;[/COLOR]

Now I have another variable:

[COLOR=DarkOrange] var MyID = “10”;[/COLOR]

I need to find out if the MyID is included in the PreSelectedIDS where each value is separated by a comma. How would I go about doing this ? Thanks !

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@hyperliskApr 02.2006 — You could use:
<i>
</i>if(PreSelectedIDS.indexOf(MyID) == -1){
alert("Aww.. we couldn't find it...");
} else {
alert("We found it!");
}
Copy linkTweet thisAlerts:
@vpekulasauthorApr 02.2006 — Seems to work, fine though isn't the indexOf similar to InSTR() where it looks if given string exists in a string ? For example where MyID = 7 and the PreSelectedIDS = "7,17,27" would it not mach all 3 values ?
Copy linkTweet thisAlerts:
@hyperliskApr 02.2006 — [i]string1[/i]indexOf([i]string2[/i]) returns the first occurence of string2 in string1. If it doesn't find string2 in string1, it returns -1. [url=http://www.webdeveloper.com/forum/showpost.php?p=543888&postcount=4]Here[/url] is my better explanation.
Copy linkTweet thisAlerts:
@vpekulasauthorApr 02.2006 — How would I go about splitting the values by comma and lopping through that to find if there is a match ? I don't think I can really use the IndexOf since it'd return funny results at times.
Copy linkTweet thisAlerts:
@hyperliskApr 02.2006 — <i>
</i>var PreSelectedIDS = "7,8,10,55,250,12";
var MyID = "10";
var splitValues = PreSelectedIDS.split(",");
// Then to loop through them...
for(var i=0;i&lt;splitValues.length;i++){
if(splitValues[i] == MyID){
alert("Found it!");
}
}
Copy linkTweet thisAlerts:
@vpekulasauthorApr 03.2006 — Thanks ?
×

Success!

Help @vpekulas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...