- Home /
Question by
karl_ · Feb 16, 2011 at 03:29 PM ·
javascriptstringsearch
string.search() in Unity Javascript?
I'm trying to get a count of the number of instances of a char in a string.
Is there any way to get this to work in Unity's JS?
var amtOfXinString : int = stringToSearch.search("x");
Comment
stringToSearch.IndexOf("x") will work in C#, and I think in Javascript.
IndexOf works, as well as LastIndexOf - but I need a total count of the times the char shows up. LastIndexOf - IndexOf was my first thought, but the characters are separated by varying amounts of letters.
Best Answer
Answer by DaveA · Feb 16, 2011 at 09:18 PM
You'd call IndexOf in a loop. Something like (untested):
int count = 0;
int i = stringToSearch.IndexOf("X");
while (i > 0)
{
count++;
i = stringToSearch.IndexOf("X",i+1);
}
Your answer
Follow this Question
Related Questions
search is not a member of string 2 Answers
How do you check if a string has already been removed from a generic list? 1 Answer
string searching question 1 Answer
string deleting 1 Answer