需要帮助在 Unity 中拆分数据字符串
Need help splitting a data string in Unity
所以在我的游戏中,我试图从数据库中获取一些文章并将数据拆分为单独的文章。这是我的代码:
IEnumerator Start () {
WWW articleData = new WWW ("http://mywebsite.com/getsomedatabase.php");
yield return articleData;
string articleDataString = articleData.text;
DevideData (articleDataString);
}
void DevideData(string data)
{
//Column 1 = article name
//Column 2 = article id
//Column 3 = option 1
//Column 4 = option 2
//Column 5 = option 3
//Column 6 = change in values that appear directly after article has come in
//Column 7 = change in values after option 1 has been chosen
//Column 8 = change in values after option 2 has been chosen
//Column 9 = change in values after option 3 has been chosen
//Column 10 = change in values after no option has been chosen
List<string> articles = new List<string> ();
string subData = data;
for (int i = 1; i < Regex.Matches (data, "|NEXT|").Count; i++) {
articles.Add(subData.Substring(0,subData.IndexOf ("|NEXT|")));
subData.Replace (subData.Substring (0, subData.IndexOf ("|NEXT|")), "");
}
}
articleDataString 基本上是一长串格式化数据,每篇文章之间用|NEXT|分隔。但是,我用来拆分字符串的方法(我什至不知道它是否有效。)对于机器来说太重了无法处理。这就是为什么我要问是否有一种更有效、更轻便的分离长字符串的方法。
在 C# 中,您可以使用字符串数组重载。像这样:
articleDataString.Split(new string[] { "|NEXT|" }, StringSplitOptions.None);
所以在我的游戏中,我试图从数据库中获取一些文章并将数据拆分为单独的文章。这是我的代码:
IEnumerator Start () {
WWW articleData = new WWW ("http://mywebsite.com/getsomedatabase.php");
yield return articleData;
string articleDataString = articleData.text;
DevideData (articleDataString);
}
void DevideData(string data)
{
//Column 1 = article name
//Column 2 = article id
//Column 3 = option 1
//Column 4 = option 2
//Column 5 = option 3
//Column 6 = change in values that appear directly after article has come in
//Column 7 = change in values after option 1 has been chosen
//Column 8 = change in values after option 2 has been chosen
//Column 9 = change in values after option 3 has been chosen
//Column 10 = change in values after no option has been chosen
List<string> articles = new List<string> ();
string subData = data;
for (int i = 1; i < Regex.Matches (data, "|NEXT|").Count; i++) {
articles.Add(subData.Substring(0,subData.IndexOf ("|NEXT|")));
subData.Replace (subData.Substring (0, subData.IndexOf ("|NEXT|")), "");
}
}
articleDataString 基本上是一长串格式化数据,每篇文章之间用|NEXT|分隔。但是,我用来拆分字符串的方法(我什至不知道它是否有效。)对于机器来说太重了无法处理。这就是为什么我要问是否有一种更有效、更轻便的分离长字符串的方法。
在 C# 中,您可以使用字符串数组重载。像这样:
articleDataString.Split(new string[] { "|NEXT|" }, StringSplitOptions.None);