我有一个这种格式的字符串" 10,2 / 20,5 / 50,3"
这些都是每个球员的胜利。 " roomPrize,胜/ nextRoomPrize,胜"等
我想出了如何阅读它,但我想改变一些roomPrize的胜利数量。我到目前为止。
winsRawData = getStatW.text; //That is the wins string formated as explained above
string[] winSplits = winsRawData.Split('/');
for(int i = 0; i < winSplits.Length; i++)
{
if(winSplits[i].Split(',')[0] == room.ToString()) //room is the roomPrize that i want to affect
{
//newWins is the new win count that I want to update with
//winSplits[i].Split(',')[1] == newWins; <-- I just need to do that
}
}
由于
答案 0 :(得分:2)
如果我理解你的问题,你想做以下事情:
winSplits[i].Split(',')[1] = newWins;
为了实现这一点,您需要拆分该值,然后重新创建它(例如,加入)并将其放回原始数组中。
以下应该可以解决问题:
winsRawData = getStatW.text; //That is the wins string formated as explained above
string[] winSplits = winsRawData.Split('/');
for (int i = 0; i < winSplits.Length; i++)
{
var dataArray = winSplits[i].Split(',');
if (dataArray[0] == room.ToString()) //room is the roomPrize that i want to affect
{
// sets the new value.
dataArray[0] = newWins;
}
// finalize by recreating the string and push it back to the original array
winSplits[i] = String.Join(",", dataArray);
}
修改使用以下值:
var winsRawData = "10,2/20,5/50,3";
int room = 10;
int newWins = 100;
通过在上面的代码末尾添加var output = String.Join("/", winSplits);
,您将得到一个字符串输出:
"100,2/20,5/50,3"
请参阅以下代码以获取完整示例:
string[] winSplits = winsRawData.Split('/');
for (int i = 0; i < winSplits.Length; i++)
{
var dataArray = winSplits[i].Split(',');
if (dataArray[0] == room.ToString()) //room is the roomPrize that i want to affect
{
// sets the new value.
dataArray[0] = newWins.ToString();
}
// finalize by recreating the string and push it back to the original array
winSplits[i] = String.Join(",", dataArray);
}
var output = String.Join("/", winSplits); // = "100,2/20,5/50,3"
答案 1 :(得分:1)
你应该首先重构将数据反序列化为一些Player对象,更改它的wins count然后序列化它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Raw data
string rawData = "10,2/20,5/50,3";
// First split
string[] playersRaw = rawData.Split('/');
// Initialize a collection for the player objects
var players = new List<Player>();
// Iterates over the splitted players data
foreach (var playerRaw in playersRaw)
{
// Deserialize each player
var playerDeserialized = Player.Deserialize(playerRaw);
// Store the player data into the players collection
if (playerDeserialized != null)
players.Add(playerDeserialized);
}
// Seeks for player with room prize 10
foreach (var player in players)
{
if (player.RoomPrize == 10)
// Increments players data wins count
player.Wins += 1;
}
// Updates the raw data serializing all players back again
rawData = string.Join("/", Array.ConvertAll(players.ToArray(), (p) => p.Serialize()));
Console.WriteLine(rawData);
Console.ReadLine();
}
}
class Player
{
public int RoomPrize { get; set; }
public int Wins { get; set; }
public static Player Deserialize(string data, char separator = ',')
{
Player player = null;
string[] splittedData = new string[] { };
if (!string.IsNullOrEmpty(data) && (splittedData = data.Split(separator)).Length == 2)
{
int roomPrize = 0, wins = 0;
if (int.TryParse(splittedData[0], out roomPrize) && int.TryParse(splittedData[1], out wins))
{
player = new Player();
player.RoomPrize = roomPrize;
player.Wins = wins;
}
}
return player;
}
public string Serialize()
{
return string.Format("{0},{1}", this.RoomPrize, this.Wins);
}
}
}
代码更多,但更容易维护。