You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

How to convert a String to Array in C#

Ben Liu
Ben Liu

Image we have a string: 123,456,78,789, we want to change it to an array, we can use the following code:

string szTmp="123,456,78,789";
string[] tmp = szTmp.Split(',');
 
for (int i = 0; i < tmp.Length; i++ )
        MessageBox.Show(tmp[i]);
 
// print out: 
// 123 
// 456
// 78
// 789

When the target string contains different separators such as: 123,456 78,789, Split support multiple separators as well:

string szTmp="123,456 78,789";
char[] char_tmp = { ',', ' '};
string[] tmp = szTmp.Split(char_tmp);
 
// print out: 
// 123 
// 456
// 78
// 789

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp