- Home /
Is it possible to split a string without specifying a separator?
Hi,
I have a word which I have selected at random from an array e.g. GARFIELD. I now want to split the word into an array. I have read that I can use .Split to do this but as there are no characters in between each letter for me to call out on which to split each letter will the below still work?
var random_word = "GARFIELD"; var random_word_array= random_word.Split(""[0]); for(i = 0; i < random_word.Length; i++){ print(random_word_array[i]); }
If not, has anyone else solved this?
Answer by Paulius-Liekis · Nov 07, 2012 at 10:07 AM
Can't you simply do `random_word[i]`?
Didn't think that I could do this in anything but arrays. Does it work in strings too?
A string is an array of Unicode-char. So just like an array you can access any index of it. You only need to make sure you are not getting out of bounds. GARFIELD shows 8 chars, well it actually has 9. The last one is a null char that deter$$anonymous$$es the end of the string .So you can type anything from 0 to 7.
Thanks, that was so simple now that you told me. $$anonymous$$uch appreciated.
@fafase: while strings in .NET/$$anonymous$$ono are technically null-ter$$anonymous$$ated, they are also length-prefixed, so the null character at the end is not accessible and not used to deter$$anonymous$$e the end of the string (inside .NET anyway).
Yep I figured that by trying it out and got an error out of bound. That is why I was saying only from 0 to 7 on a 8 char word despite the 9th one. Well, I would guess no one is really trying to print out the null character though.
Your answer

Follow this Question
Related Questions
Can't use remove with string[] ? 1 Answer
Iterating through multidimensional arrays 2 Answers
Comparing a string to an array extraction? 1 Answer
Get JSON array object string value 2 Answers