C# 函数来定位文本中的字符串
在文本中搜索特定字符串,然后只显示其中的一部分,最有效的方法是什么 以下是我的情况:我目前正在我的服务器上托管一个.txt文件。我想要创建的函数将访问这个.txt(甚至可能下载以提高效率?),搜索一个ID(例如300000000),然后将名称放入字符串(例如Island Andrew) 以下是托管在我的服务器上的.txt文件的示例:C# 函数来定位文本中的字符串,c#,visual-c++,C#,Visual C++,在文本中搜索特定字符串,然后只显示其中的一部分,最有效的方法是什么 以下是我的情况:我目前正在我的服务器上托管一个.txt文件。我想要创建的函数将访问这个.txt(甚至可能下载以提高效率?),搜索一个ID(例如300000000),然后将名称放入字符串(例如Island Andrew) 以下是托管在我的服务器上的.txt文件的示例: ID: 300000000 NAME: Island Andrew ID: 300000100 NAME: Island Bob ID: 300000010 NAM
ID: 300000000 NAME: Island Andrew
ID: 300000100 NAME: Island Bob
ID: 300000010 NAME: Island George
ID: 300000011 NAME: Library
ID: 300000012 NAME: Cellar
我已经为一个类似的例子编写了完整的代码,但是,格式是不同的,它不是用c#编写的
在这里
如果有人能帮我用c#完成这项工作,我将不胜感激
谢谢。- 删除所有插入符号(^)
- 将所有成员访问运算符(->)转换为点
- 将
更改为gcnew
将数组转换为new
string[]
- 从类中删除私有修饰符和公共修饰符,并将它们放在方法上
- 显式(例如,
)公共void CacheMaps()
- 将
更改为ref class
static class
- 将
更改为nullptr
null
- 将
更改为仅catch(…)
catch
- 使用名称空间将
移动到文件的最顶端,并将范围解析运算符(::)替换为点
应该就是这样。您可以尝试用C#创建一个名称数组:
字典映射字典;
string[]mapNames=rawData.Split(splitChar,StringSplitOptions.None);
foreach(映射名中的字符串str)
{
{
字符串mapid=str.Substring(str.IndexOf(“:”);
字符串mapname=str.Remove(0,str.IndexOf(':')+1);
mapDictionary.Add(Convert.ToInt32(mapid),mapname);
}
}
最简单的方法是在ID:30000和Name:Andrew Island之间做一个令牌分隔符,并删除ID和Name本身
30000, Andrew Island
然后在C代码中创建一个名为
public class SomeDTO {
public long ID{get; set;}
public string Name {get; set;}
}
接下来,您将创建一个SomeDTO类型的新列表,如下所示:
var List = new List<SomeDTO>();
现在,您已经将整个列表存储在内存中,您可以执行一系列搜索和其他操作,找到您需要的所有内容,并重新使用它,因为您已经构建了列表
var first = List.Where(x => x.Name.Equals("Andrew Island")).FirstOrDefault();
没有正确错误处理的简单方法。 我们要看的主要部分是正则表达式
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class Program
{
static void Main()
{
var map = new Map();
Console.WriteLine(map[300000011]);
}
}
public class Map: Dictionary<int, string>
{
public Map()
{
WebClient wc = new WebClient()
{
Proxy = null
};
string rawData = wc.DownloadString("<insert url with data in new format here>");
PopulateWith(rawData);
}
void PopulateWith(string rawText)
{
string pattern = @"ID: (?<id>\d*) NAME: (?<name>.*)";
foreach (Match match in Regex.Matches(rawText, pattern))
{
// TODO: add error handling here
int id = int.Parse( match.Groups["id"].Value );
string name = match.Groups["name"].Value;
this[id] = name;
}
}
}
使用系统;
Net系统;
使用System.Text.RegularExpressions;
使用System.Collections.Generic;
班级计划
{
静态void Main()
{
var map=newmap();
Console.WriteLine(地图[30000011]);
}
}
公共类映射:字典
{
公共地图()
{
WebClient wc=新的WebClient()
{
代理服务器=null
};
字符串rawData=wc.DownloadString(“”);
用(原始数据)填充;
}
无效填充(字符串文本)
{
字符串模式=@“ID:(?\d*)名称:(?*);
foreach(Regex.Matches中的匹配(rawText,pattern))
{
//TODO:在此处添加错误处理
int id=int.Parse(match.Groups[“id”].Value);
字符串名称=匹配。组[“名称”]。值;
此[id]=名称;
}
}
}
这是一个纯粹的Visual C++到C转换问题,它与WiFras无关,我正在更新标签。你的意思是“然后只显示其中的一部分”?要显示字符串/文本的哪一部分?与你的id匹配的那个?如果我搜索300000000,它会将Island Andrew放入字符串@Grozz所有当前的答案似乎都不符合我提供的格式…这是否适用于我服务器上的.txt文件格式?如果是这样,这就是我问题的正确答案!是的,我相信会的。这基本上就是你转换成c#的代码。赢家鸡肉晚餐。使用新的格式!谢谢
var first = List.Where(x => x.Name.Equals("Andrew Island")).FirstOrDefault();
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class Program
{
static void Main()
{
var map = new Map();
Console.WriteLine(map[300000011]);
}
}
public class Map: Dictionary<int, string>
{
public Map()
{
WebClient wc = new WebClient()
{
Proxy = null
};
string rawData = wc.DownloadString("<insert url with data in new format here>");
PopulateWith(rawData);
}
void PopulateWith(string rawText)
{
string pattern = @"ID: (?<id>\d*) NAME: (?<name>.*)";
foreach (Match match in Regex.Matches(rawText, pattern))
{
// TODO: add error handling here
int id = int.Parse( match.Groups["id"].Value );
string name = match.Groups["name"].Value;
this[id] = name;
}
}
}