如何读取文件中的特定单词

编程入门 行业动态 更新时间:2024-10-28 15:27:39
本文介绍了如何读取文件中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从文本文件中读取特定的单词。例如 AAA.txt 是我的文本文件,它包含如下所示的单词

Hi, I want to read a particular word from a text file. For example AAA.txt is my text file and it contains words as shown below

Name := Dell Server := Dataserver User := Mike

我想要的数据是Dataserver。我怎么得到它?

The data i want is Dataserver. How do I get it?

推荐答案

会帮助你 string strvalue = string.Empty; using(System.IO.StringReader sr = new StringReader(c:\ aaa.txt)) { string [] strdata = sr.ReadToEnd()。Split(new char [] {'\ n'}); foreach(strdata中的字符串项) { if(item.Contains(Server:=)) { strvalue = item.Replace(Server:=,) ; 休息; } } will Help you string strvalue = string.Empty; using (System.IO.StringReader sr = new StringReader("c:\aaa.txt")) { string[] strdata = sr.ReadToEnd().Split(new char[] { '\n' }); foreach (string item in strdata) { if (item.Contains("Server :=")) { strvalue = item.Replace("Server :=",""); Break; } }

你们真的用了 Key:= Value 或更确切地说 key = value ?在最后一种情况下,您可以将Windows API用于ini文件。 否则,您必须逐行读取文件并使用正则表达式解析每一行。 Do you really use Key :=Value or rather key=value ? In the last case, you can use the Windows API for ini-files. Otherwise, you have to read the file line by line and parse each line with a regex.

使用下面的代码获取file.txt中的指定字符串 之后你删除字符串=; Use the below code to get the specified string in the file.txt After that you remove the strings upto "="; string regexMatchString= "Server :=Dataserver"; string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\Textfile.txt"); string matchedString = Regex.Match(text, regexMatchStrig).ToString();

更多推荐

如何读取文件中的特定单词

本文发布于:2023-10-04 21:08:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!