【发布时间】:2019-08-31 11:36:28
【问题描述】:
大家好,我是一名学生,还在学习 C#。我需要解析一个具有这种格式的字符串:
< test, 1, 0, 1>
如何提取单词test、数字1、0 和1,以将它们放入适当数据类型的变量中?
我尝试将其转换为string,然后使用Substring() 和IndexOf() 和Split(),但它们都不起作用。
//this is what i did in c but i cant do it in c#
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
com1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
com2 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
com3 = atoi(strtokIndx);
//strtokIndx = strtok(NULL, ",");
//com4 = atof(strtokIndx);
}
【问题讨论】:
-
字符串究竟是什么样的?
"<test,1,0,1>"还是"test,1,0,1"?我的意思是<和>是字符串的一部分,还是您只是添加它们来标记字符串? -
@RenéVogt 它旨在成为字符串的一部分,因为它标志着它的开始和结束。
-
好的,相应地更新了我的答案
标签: c# arrays string parsing char