使用空格检查

时间:2016-10-15 10:54:54

标签: c# string parsing substring console-application

您好我正在使用控制台应用程序,我正在读取字符串并获取我的值:

字符串:

 Information                                                                                                                        
First Name: WORLD    Middle: HELLO Last Name: PQRS
 
CalBRE: 012345678 Login ID: IVACOSEST
Address: 12345 ABCd RD.
 
City: ABCd XYZ State: California Zip: 12345                                                           
Direct Office Ph: 123-456-7890 Cell Phone: 123-456-7890
Home Phone: Fax: 123-456-7890
Toll Free Phone: Pager:
Voicemail:
Email: abcd@abcd.net
Office Information                                                                                                                                                   
Office Name: Offc. Office Code: IVNEXT
Address: 165 ABC XYZ LANE #10 Office CalBRE: 0123456
City: San Fransisco Zip: 12345
Phone: 123-456-7890 Fax: 123-456-7890

从我上面的字符串中我想读取如下数据:

中东

名字

DirectOffice

我想将数据作为FirstName读取,而不是从名字开始直到获得空白区域,请告诉我任何人如何做?

我刚刚完成了以下代码:

var stIndex = String[i].IndexOf("FirstName") + (FirstName).Length;

String有成千上万的记录,所以我把每个数组都放在循环中,就像我上面提供的那样。

在上面的示例代码中,我得到了起点但是如何获得最后一点:

我的意思是我想读FirstName,所以我得到名字的起点,但是如何读取名字:价值,即WORLD直到白色空间!

3 个答案:

答案 0 :(得分:0)

如何阅读First Name:和Middle之间的所有字符:然后修剪结果字符串以获取名字的值。

int startIndex = String[i].IndexOf("First Name") + ("First Name:").Length;
int middleNameStartIndex = String[i].IndexOf("Middle");

string FirstNameValue = String[i].Substring(startIndex, middleNameStartIndex - startIndex).trim();

与其他所有人类似,你知道按键的位置,这样你就可以为所有其他人执行类似的操作......

答案 1 :(得分:0)

我在字符串中搜索的每个列表项都定义了一个数组,如下所示,

string a = "your text";
List<string> results = new List<string>();
string[] searchList = { "First Name:", "Middle:", "Last Name:", "Direct Office Ph:" };

foreach (var item in searchList)
        {
            int indexStarts = a.IndexOf(item); // first whitespace
            a = a.Substring(indexStarts + item.Length + 1);
            if (a.Substring(a.IndexOf(" "), a.IndexOf(" ")).Trim() == "-") // in the phone seperator we need to approach another solution so checking - character
            {
                string result = "";
                while (a.Substring(a.IndexOf(" "), a.IndexOf(" ")).Trim() == "-")
                { // if the following string between whitespaces has - we need to loop
                    result += a.Substring(0, a.IndexOf(" ") + 3); //white space - whitespace so 3 character it will generate 123 - then 456 - ..
                    a = a.Substring(a.IndexOf(a.Substring(0, a.IndexOf(" ") + 3)) + a.Substring(0, a.IndexOf(" ") + 3).Length); 
// after taking numbers 123 -, 456 - it will reproduce the text and making start a new startpoint before - and whitespaces

                }
                result += a.Substring(0, a.IndexOf(" ") + 1); // if while loop ends it means there is one whitespace at the 7890 Cell Phone: behind 0 and C so we need to substring it.
                results.Add(item + " " + result); // add to list
            }
            else
            {
                results.Add(item + " " + a.Substring(0, a.IndexOf(" ") + 1)); // this one add directly (for firstname,middle,lastname)
            }

        }

这是结果(顺便说一句,它仅适用于一个字符串,仅用于逻辑),

enter image description here

希望有所帮助,

答案 2 :(得分:0)

我会从下面的代码开始。您输入解析非常棘手,需要自定义解析器。由于以下原因,这很棘手 1)一行上有多个属性 2)属性名称是多个单词,中间有空格 3)值是多个单词,其间有空格 4)某些属性没有值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            string inputLine = "";
            Person newPerson = null;
            while((inputLine = reader.ReadLine()) != null);
            {
                inputLine = inputLine.Trim();
                if (inputLine.Length > 0)
                {
                    if (inputLine.Contains("Information"))
                    {
                        newPerson = new Person();
                        Person.peopel.Add(newPerson);
                    }
                    else
                    {
                        //enter code to parse data
                    }
                }
            }
        }
    }
    public class Person
    {
        public static List<Person> peopel = new List<Person>();
        public Dictionary<string, string> properties = new Dictionary<string, string>();
    }
}