Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用不带文本限定符的split()方法_C#_.net_String_Split_Streamreader - Fatal编程技术网

C# 使用不带文本限定符的split()方法

C# 使用不带文本限定符的split()方法,c#,.net,string,split,streamreader,C#,.net,String,Split,Streamreader,我正在尝试使用streamReader从文本文件中获取一些字段值 要读取自定义值,我使用split方法。我的分隔符是冒号“:”,我的文本格式如下所示: Title: Mytitle Manager: Him Thema: Free ..... Main Idea: best idea ever ..... 我的问题是,当我尝试获取第一个字段时,即标题,我使用: string title= text.Split(:)[1]; 我得到title=MyTitle经理 而不仅仅是:titl

我正在尝试使用streamReader从文本文件中获取一些字段值

要读取自定义值,我使用split方法。我的分隔符是冒号“:”,我的文本格式如下所示:

Title: Mytitle 
Manager: Him 
Thema: Free 
.....
Main Idea: best idea ever 
.....
我的问题是,当我尝试获取第一个字段时,即标题,我使用:

 string title= text.Split(:)[1];
我得到title=MyTitle经理

而不仅仅是:title=MyTitle。 任何建议都很好

我的文本如下所示:

My mail : ........................text............

Manager mail : ..................text.............

Entity :.......................text................

Project Title :...............text.................

Principal idea :...................................

Scope of the idea : .........text...................

........................text...........................

Description and detail :................text.......
..................text..... 
Cost estimation :..........
........................text...........................
........................text...........................
........................text...........................

Advantage for us :.................................
.......................................................

Direct Manager IM :................................
根据您的帖子更新

//I would create a class to use if you haven't
//Just cleaner and easier to read
public class Entry
{
    public string MyMail { get; set; }
    public string ManagerMail { get; set; }
    public string Entity { get; set; }
    public string ProjectTitle { get; set; }
    // ......etc
}

//in case your format location ever changes only change the index value here
public enum EntryLocation
{
    MyMail = 0,
    ManagerMail = 1,
    Entity = 2,
    ProjectTitle = 3
}

//return the entry
private Entry ReadEntry()
{
    string s =
        string.Format("My mail: test@test.com{0}Manager mail: test2@test2.com{0}Entity: test entity{0}Project Title: test project title", Environment.NewLine);

    //in case you change your delimiter  only need to change it once here
    char delimiter = ':';

    //your entry contains newline so lets split on that first
    string[] split = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

    //populate the entry
    Entry entry = new Entry()
    {
        //use the enum makes it cleaner to read what value you are pulling
        MyMail = split[(int)EntryLocation.MyMail].Split(delimiter)[1].Trim(),
        ManagerMail = split[(int)EntryLocation.ManagerMail].Split(delimiter)[1].Trim(),
        Entity = split[(int)EntryLocation.Entity].Split(delimiter)[1].Trim(),
        ProjectTitle = split[(int)EntryLocation.ProjectTitle].Split(delimiter)[1].Trim()
    };

    return entry;
}

这是因为split返回由指定符号分隔的字符串。就你而言:

Title
Mytitle Manager
Him
.1。您可以更改数据格式以获得所需的值,例如:

Title: Mytitle:Manager: Him
在这里,第二个元素将是值

text.Split(:)[1] == " Mytitle";    
text.Split(:)[3] == " Him";
.2。或者,您可以调用text.Split“”,“:”以获得相同的名称-值对列表,而无需更改格式

.3。此外,如果您的数据分别放在文件中的新行上,如:

Title: Mytitle
Manager: Him
您可以将内容流式传输到单个字符串中,然后还可以执行以下操作:

text.Split(new string[] {Environment.NewLine, ":"}, StringSplitOptions.None);

老实说,我不明白为什么你-我。如果他能改变数据格式,他只需要一个分割符号就可以达到同样的效果。嘿,Alex,谢谢你的回答,我会尝试改变文本格式。我没有放任何-1:我想如果它是一个流内容,并且你的每个语句都放在一个新行上,那么你一定是缺少了Environment.NewLine常量。谢谢你的回答,但是Title=Mytitle和Manager=HIM不在同一行。但我理解你的回答:@Thim你的帖子提出了相反的建议。如果您能准确地发布它的外观,那么我们可以更好地为您提供帮助。@Thim那么您的每个值都在它自己的行中?请显示您是如何读取这些值的。对于第一个值,请使用string title=textfile.Split:[1]作为示例。因此,通过更改索引,我一行一行地获取值。@Thim请准确显示textfile var中的内容