- Home /
using array functions on a string?
is there a way to use the array functions on a string, such as push, pop, shift, and all that?
i tried to convert from string to char[] but it that doesnt work.
i'm making a text adventure styled game and im going to need much better control of my UI.Text's strings. and array functions would help alot.
i tried to convert from string to char[] but it that doesnt work.
What does not work about it? You convert the string to a char[] with the string function ToCharArray and then add all the elements to your javascript Array.
Also, if you are doing lots and lots of changes to strings, you might want to look into StringBuilder for these reasons
Answer by HarshadK · Jan 28, 2015 at 08:26 AM
You can convert a string to char array using ToCharArray as specified by GameVortex in his comment under the question.
Something like:
string myString = "This is a string";
char[] stringCharArray;
void Start()
{
stringCharArray = myString.ToCharArray();
// This will just print the characters from the array one by one
for(int i = 0; i < stringCharArray.Length; i++)
{
Debug.Log("Char at " + i + " is: " + stringCharArray[i]);
}
}
If this is not working for you there is also an alternate method as below:
string myString = "This is a string";
char[] stringCharArray;
void Start()
{
stringCharArray = new char[myString.Length];
// Add each character from string to the array
for(int i = 0; i < myString.Length; i++)
{
stringCharArray[i] = myString[i];
}
}
I think you can do the the rest push, pop, shift and other things with this array.
oh wow, i didn't know about that function. thanks
also as a related side question, after iv edited the string with the arrays functions, how do i convert back to the String?
Your answer
![](https://koobas.hobune.stream/wayback/20220613181737im_/https://answers.unity.com/themes/thub/images/avi.jpg)