从.TXT文件中检索字符串,然后在C#中解析它们 [英] Retrieve strings from a .TXT file and parse them in C#
本文介绍了从.TXT文件中检索字符串,然后在C#中解析它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我是编程的新手,并且正在学习视频课程以学习C#.
我需要一点帮助来进行此练习:
Hello everybody,
I''m quite new to programming, and I''m following a video-course in order to learn C#.
I need a little help to do this exercise:
- 使用
string
打开 .TXT 文件,如下所示:
- Open a .TXT file with
string
s like this:
+01/01/1918 11:59:59-1-2-3+
string
(我以为一个),并获取以"-
"定界符开头的数字(在这种情况下,数字为
1 2 3
)string
string
s (one by one I presume) and get the numbers preceded by the "-
" delimiter(in this case, the numbers:
1 2 3
)string
谢谢您的帮助.
Thank you for your help.
推荐答案
1)修剪前导/尾随''+:myLine = myLine.Trim(new char[] {''+''})
;
2)用``-''分隔每行:使用:char[] terms = myLine.Split(new char[] {''-''})
;
3)拆分将为您提供4个字符的char数组;使用System.DateTime.Parse(string, IFormatProvider, DateTimeStyles)
或System.DateTime.ParseExact(string, string, IFormatProvider)
将第一个(terms [0])解析为System.DateTime
;确保使用适当的CultureInfo
作为IFormatProvider
的实例,以匹配特定于区域性的日期/时间格式(从您的示例中,您无法了解哪个数字是月份,哪个是天,但是您应该从区域性中了解这一点) ;在第二种情况下,请使用适当的显式格式字符串(请参阅下面的参考资料).
4)使用您的类型解析其他术语;由于-"是分隔符而不是符号,因此您的类型是无符号的;所以可能是byte, ushort, uint, ulong
;在所有情况下,请使用Parse
或TryParse
,例如uint.Parse(term[index])
,其中索引为1到3.
参见:
http://msdn.microsoft.com/en-us/library/system.string.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].
-SA
1) Trim leading/trailing ''+'':myLine = myLine.Trim(new char[] {''+''})
;
2) Split each line by ''-'': use:char[] terms = myLine.Split(new char[] {''-''})
;
3) The split will give you array of char with 4 terms; parse first one (terms[0]) toSystem.DateTime
usingSystem.DateTime.Parse(string, IFormatProvider, DateTimeStyles)
orSystem.DateTime.ParseExact(string, string, IFormatProvider)
; make sure to use appropriateCultureInfo
as an instance ofIFormatProvider
to match culture-specific date/time format (from your example, one cannot understand which number is month and which is day, but you should know this from the culture); in second case, use appropriate explicit format string (see references below).
4) Parse other terms using your type; as ''-'' is a delimiter and not sign, your type is unsigned; so it could bebyte, ushort, uint, ulong
; in all cases, useParse
orTryParse
, for exampleuint.Parse(term[index])
, where your index is 1 to 3.
See:
http://msdn.microsoft.com/en-us/library/system.string.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].
—SA
static void Main(string[] args)
{
string text = "+01/01/1918 11:59:59-1-2-3+";
string[] words = text.Split(''-'');
List<string >buffer=new List<string>();
//Initial vords
foreach (var word in words)
{
Console.WriteLine(word);
buffer.Add(word);
}
buffer.Remove(buffer[0]);//remove first words
buffer[2] = buffer[2].Remove(buffer[2].Length - 1, 1);//remove simbol +
//Show final list with result : 1, 2 , 3
foreach (var aux in buffer)
{
Console.WriteLine(aux);
}
Console.ReadLine();
}
取决于格式的其余部分是否始终是安全的(事实并非如此) t包含-digit),则可以使用正则表达式-\ d *"提取所有数字.如果它们总是彼此相邻,则可以使用(-\ d *)+",该字符将匹配一次,并且其内部组将是每个数字.
Depending on whether the rest of the format is always safe (it doesn''t contain -digit), you can extract all the numbers with a regex "-\d*". If they are always right next to each other you can use "(-\d*)+", which will match once, and its inner groups will be each number.
这篇关于从.TXT文件中检索字符串,然后在C#中解析它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文