Hi guys, how can I convert a string in char a[ ] ?? thanks
Here’s one way:
string myString = "SomeTestString";
char[] myChars = myString.ToCharArray();
Ok, but is there any possibility to display myChars ?
Cause if I made : myChars.ToString() it returns “System.Char[ ]”
string s = new string(myChars);
Thanks!
Well, can we do random sorting while convert a string to char? For example: “string” will convert to (“r”,“t”,“n”,“g”,“s”,“i”) not (“s”,“t”,“r”,“i”,“n”,“g”)
That’s two separate issues. To shuffle a string you can just use something like this:
public static class StringExtensions
{
public static string Shuffle(this string str)
{
if (str == null || str.Length < 2)
return str;
string newString = "";
int strLength = str.Length;
int[] rndArray = new int[strLength];
for (int i = 0; i < strLength; i++)
rndArray[i] = i;
System.Random rnd = new System.Random();
int random;
int temp;
for (int i = 0; i < strLength; i++)
{
random = i + (int)(rnd.NextDouble() * (strLength - i));
temp = rndArray[random];
rndArray[random] = rndArray[i];
rndArray[i] = temp;
}
for (int i = 0; i < strLength; i++)
newString += str[rndArray[i]];
return newString;
}
}
So, to convert a string into a shuffled char array, just do:
Char[] newCharArray = someString.Shuffle().ToCharArray();
Or, alternatively you can shuffle the char array after it’s made using absolutely any array shuffling algorithm on the net, of which there are hundreds, including the one in the middle of the string shuffle function above (Fisher-Yates).
You can do it like this:
Debug.Log(new string("string".OrderBy(c => Random.value).ToArray()));
I think I’ll use linq right?
Right.
A string is represented by a dynamic “char array” internally. In order to access a specific char from string, you can do “Hello”[1] which returns ‘e’. If you need read access to the chars of a string, you don’t have to convert it to a char array.
Using Linq produces (often) very inefficient code. Unity and Microsoft recommend to avoid using Linq in applications where performance is a concern.
https://developer.microsoft.com/en-us/windows/mixed-reality/performance_recommendations_for_unity
Your post sounds like the device running linq could explode
I actually used to warn about performance of linq like you do, but now I put it to “premature optimization” category.
wow. it works. thanks for help but there is one more something. I have to put the chars to UI Texts. So you know scramble games don’t you? I have letter slots and each slots has UI texts. you can look the picture.
Then how can i do put the chars to UI Texts? I think I have to integrate char array but I couldn’t.
Me or somebody else will help you with this, but you should start a new thread.
when i start a new thread, nobody looking it. I have to spamming because of this
If you describe the problem properly, it’ll be answered quickly enough (unless you come up with some really special and complicated topic that noone has a clue about - which is not the case here).
I describe the problem properly, no problem. Relax