如何从文本文件中读取3个变量? [英] how to read 3 variables from text file?
问题描述
请问我愚蠢的问题
我想读取3个变量x,y和z,它们分别写在文本文件"myfile.txt"中的一条水平线上
像这样
123 456 7890
我已经阅读了
的字符串
myString = MyStreamReader.ReadLine();
变量之间有空格,我不知道变量之间有多少空格
x和y以及y和z
如果有问题
myString.Split无法正常工作(如果Split可以完成这项工作,请让我知道如何操作),我也不会更喜欢
myString.Substring(id,len)
因为每个变量的长度都是不可预测的,所以
String.Split
可行,因此请指定RemoveEmptyEntries
选项,例如:
字符串 str = " ; 字符串 [] snum = str.Split(新 char [] {' '},StringSplitOptions.RemoveEmptyEntries); int [] v = 新 int [ snum.Length]; for ( int n = 0 ; n < v.Length; n ++) { v [n] = int .Parse(snum [n]); }
尝试:
string[] numbers = myString.Split(new char[] { '' '', ''\t'' }, StringSplitOptions.RemoveEmptyEntries);
如果可以确保在变量之间存在空格,则可以用空格分割该字符串,然后只能使用值不为''的那些字符串.
excuse my silly question
i want to read 3 variables x,y and z ,they are written in one horizontal line in text file "myfile.txt"
like this
123 456 7890
i have read the string by
myString=MyStreamReader.ReadLine();
there are spaces between the variables ,i do not know how many spaces between the variable
x and y , and y and z
if guss
myString.Split will not works(if Split can do the job please let me know how),i also will not prefere the
myString.Substring(id,len)
because the length of each variable is unpredected
String.Split
works, provide you specify theRemoveEmptyEntries
option, for instance:
string str = "123 456 7890"; string[] snum = str.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries); int[] v = new int[snum.Length]; for (int n = 0; n < v.Length; n++ ) { v[n] = int.Parse(snum[n]); }
Try:
string[] numbers = myString.Split(new char[] { '' '', ''\t'' }, StringSplitOptions.RemoveEmptyEntries);
If there are spaces for sure between variables, you can split that string with space and than you can use only those strings whose value is not ''''.
这篇关于如何从文本文件中读取3个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!