wal*_*eed 0 c# unity-game-engine
我是c#unity的新手.我要在firebase中保存我的位置数组,因为我正在创建一个像int这样的int数组
int[] positions = new int[] {2, 4, 3};
Run Code Online (Sandbox Code Playgroud)
它的工作正常,但我不知道如何将其转换为字符串数组,"[2, 4, 3]"以保存在firebase中.
我在谷歌搜索并尝试过
string stringPositions = string.Join("", positions);
但它完全将我的数组转换为字符串234.还有如何将此字符串再次编码为数组.如果有任何其他方法可以让我知道.谢谢!
首先你的问题是错的,你想将int数组转换为字符串.
用这个:
int[] positions = new int[] {2, 4, 3};
string result = "[" + string.Join(",", positions) + "]";
Run Code Online (Sandbox Code Playgroud)
或这个:
int[] positions = new int[] {2, 4, 3};
StringBuilder stb = new StringBuilder();
stb.Append("[");
stb.Append(string.Join(",", positions));
stb.Append("]");
string result = stb.ToString();
Run Code Online (Sandbox Code Playgroud)
或者如果你有C#6或更高:
int[] positions = new int[] {2, 4, 3};
string result = $"[{string.Join(",", positions)}]";
Run Code Online (Sandbox Code Playgroud)
此外,如果你想转换回你的int阵列,例如你可以写你的转换器:
private int[] ConvertToIntArray(string myCustomString) //myCustomString is in "[1,2,3]" format
{
return myCustomString.Substring(1, myCustomString.Length - 2)
.Split(',')
.Select(s => int.Parse(s))
.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |