- Home /
Find all strings beginning with specified characters
Hi there, I've been trying to grab a list of all the PlayerPref strings that beging with "player". I found that:
var test = stringToSearch.IndexOf("player");
is a possible way of searching for strings, but i'm not entirely sure on the way in which this works.
or even substring method.
Anyone know how I can achieve this? Thanks - C
Answer by CHPedersen · Jun 20, 2011 at 09:59 AM
IndexOf
returns the index of the first occurrence of the string you supply. A string in .Net is actually implemented as a character array, so when the method says "IndexOf", it really means the index of the first character in the array that is the string, where the substring you're searching for was first found.
So if you have this string:
string example = "This_string_says_player_in_it";
And you do this:
int index = example.IndexOf("player");
Then the variable index
gets set to 17, because that's the array index position of the 'p' in "player". Because strings are really character arrays, you can actually index into a string directly, if you wanted to:
char indexedInto = example[17];
Is perfectly valid code, and sets the char indexedInto
to the value 'p', because as we've previously shown, that's the character found at index position 17.
That's how strings and IndexOf works, anyway. I'm not sure how you could use that to obtain all PlayerPref strings that begin with the substring "player". Are you using the method "`PlayerPrefs.GetString()`"?
Thanks for the breakdown, I'm using 'PlayerPrefs.GetString()' method, I've tried various things such as 'StartWith' function which is also valid, but it's not valid with the function 'PlayerPrefs.GetString()', I will see how I could work with 'IndexOf'.
Your answer
Follow this Question
Related Questions
Search a substring in an array of Strings 1 Answer
IndexOf returning -1 c# 2 Answers
Vector3 arrays in C# 0 Answers