使用没有文本限定符的split()方法

时间:2014-07-22 14:12:56

标签: 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 Manager

而不只是: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 :................................

2 个答案:

答案 0 :(得分:1)

根据您的帖子更新

//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;
}

答案 1 :(得分:-1)

这是因为split返回由您指定的符号分隔的字符串。在你的情况下:

Title
Mytitle Manager
Him

0.1。您可以更改数据格式以获得所需的值,例如:

Title: Mytitle:Manager: Him

每个第二个元素都是值。

text.Split(:)[1] == " Mytitle";    
text.Split(:)[3] == " Him";

0.2。或者您可以调用 text.Split('',':')来获取相同的名称 - 值对列表,而无需更改格式。

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

Title: Mytitle
Manager: Him

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

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