x


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");
more ▼

asked Feb 16 '11 at 03:29 PM

karl_ gravatar image

karl_
2.4k 41 53 70

stringToSearch.IndexOf("x") will work in C#, and I think in Javascript.

Feb 16 '11 at 05:04 PM yoyo

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.

Feb 16 '11 at 07:42 PM karl_
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Feb 16 '11 at 09:18 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x419
x44

asked: Feb 16 '11 at 03:29 PM

Seen: 2799 times

Last Updated: Feb 16 '11 at 03:29 PM