如何从c#.net中的STRING PARSE DATA [英] HOW to PARSE DATA from a STRING in c#.net

查看:55
本文介绍了如何从c#.net中的STRING PARSE DATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我有一个像这样的文字..



商品ID: ABC 
交易日期:27-JUL-2017
交易时间:2017年7月27日星期四08:33:57 GMT
交易类型:SPOTTER
货币对:INR
客户用户:VIKAS

分配: -
账户:LOCAL
卖出:INR 500
买入:INR 200
价值日期:2017年7月31日
全包价:1.5
即期汇率:1.5

分配: -
账户:海外
卖出:INR 100
买入:INR 50
价值日期:2017年7月31日
全包率:1.2
即期汇率:1.2







我想在我的GIVEN表格中显示数据..

交易ID交易日期交易时间交易类型货币配对客户用户
------------------------------------------- -------------------------------------------------- -----
ABC 27-JUL-2017 2017年7月27日星期四08:33:57 GMT SPOTTER INR VIKAS




账户卖出买入价值日期全价率现货价格
-------------------------------------- -------------------------------------------------- ----------
当地INR 500 INR 200 2017年7月31日1.5 1.5
海外100卢比50 2017年7月31日1.2 1.2

< br $> b $ b



请帮助我如何从中分配数据文字输入C#



我尝试过:



 string Word; 
Console.WriteLine(输入单词!..);
Word = Console.ReadLine(); //在运行时从User读取输入字符串
var Value = Word.Split(''); //使用'Space'拆分字符串并将其存储为var变量
Dictionary< string,int> RepeatedWordCount = new Dictionary< string,int>();
for(int i = 0; i< Value.Length; i ++)//循环拆分字符串
{
if(RepeatedWordCount.ContainsKey(Value [i]))//检查如果word已存在于字典中,则更新计数
{
int value = RepeatedWordCount [Value [i]];
RepeatedWordCount [Value [i]] = value + 1;
}
其他
{
RepeatedWordCount.Add(Value [i],1); //如果一个字符串重复而没有添加到字典中,这里我们要添加
}
}
Console.WriteLine();
Console.WriteLine(------------------------------------);
Console.WriteLine(重复的单词和计数);
foreach(KeyValuePair< string,int> kvp in RepeatedWordCount)
{
Console.WriteLine(kvp.Key +Counts are+ kvp.Value); //打印重复的单词及其计数
}

解决方案

看起来你使用了错误的分隔符,即''(空白)在文本中拆分字符串。你应该使用':'(冒号)。


试试



 string [] lines = File.ReadAllLines(@D:\TextFile1.txt); 
Dictionary< string,string> dictTrade = new Dictionary< string,string>();
List< Dictionary< string,string>> lstDictAllocations = new List< Dictionary< string,string>>();
int i = 0;
foreach(行中的字符串行)
{

string _line = line.Trim();
if(_line.Length == 0)
break;
i ++;
string [] splitter = _line.Split(':');
string key = splitter [0];
string value = splitter.Length == 2? splitter [1]:string.Join(:,splitter.Skip(1));
dictTrade.Add(key.Trim(),value.Trim());
}


var linesAlloc = string.Join(\ n,lines.Skip(i))。Split(new string [] {Allocation: - },StringSplitOptions.RemoveEmptyEntries);
foreach(linesAlloc中的字符串行)
{
string _line = line.Trim();
if(_line.Length!= 0)
{
Dictionary< string,string> dictAllocation = new Dictionary< string,string>();
var innerLines = _line.Split('\ n');
foreach(innerLines中的字符串内部)
{

string [] splitter = inner.Split(':');
string key = splitter [0];
string value = splitter.Length == 2? splitter [1]:string.Join(:,splitter.Skip(1));
dictAllocation.Add(key.Trim(),value.Trim());

}
lstDictAllocations.Add(dictAllocation);
}

}



注意:将格式化到表格中并写入文本文件


HI,
I HAVE A TEXT IN STRING LIKE THIS..

Trade ID:                ABC
Trade Date:              27-JUL-2017
Trade Time:              Thu Jul 27 2017 08:33:57 GMT
Trade Type:              SPOTTER
Currency Pair:           INR
Customer User:           VIKAS

Allocation:-
Account:            LOCAL
                                         SELL:               INR 500
                                         BUY:                INR 200
                                         Value Date:         31-JUL-2017
                                         All In Rate:        1.5
                                         Spot Rate:          1.5

                                    Allocation:-
                                         Account:            OVERSEASE
                                         SELL:               INR 100
                                         BUY:                INR 50
                                         Value Date:         31-JUL-2017
                                         All In Rate:        1.2
                                         Spot Rate:          1.2




AND I WANT TO SHOW THE DATA IN MY GIVEN FORMATE..

Trade ID	Trade Date		Trade Time						Trade Type	Currency Pair	Customer User
--------------------------------------------------------------------------------------------------
ABC 		27-JUL-2017		Thu Jul 27 2017 08:33:57 GMT	SPOTTER		INR				VIKAS




Account			SELL		BUY			Value Date		All In Rate		Spot Rate
--------------------------------------------------------------------------------------------------
LOCAL			INR 500		INR 200		31-JUL-2017		1.5				1.5
OVERSEASE		INR 100		INR 50		31-JUL-2017		1.2				1.2




PLEASE HELP ME HOW TO PARSE DATA FROM TEXT IN C#

What I have tried:

string Word;
            Console.WriteLine("Enter the word!..");
            Word = Console.ReadLine();   // Read the Input string from User at Run Time  
            var Value = Word.Split(' ');  // Split the string using 'Space' and stored it an var variable  
            Dictionary<string, int> RepeatedWordCount = new Dictionary<string, int>();
            for (int i = 0; i < Value.Length; i++) //loop the splited string  
            {
                if (RepeatedWordCount.ContainsKey(Value[i])) // Check if word already exist in dictionary update the count  
                {
                    int value = RepeatedWordCount[Value[i]];
                    RepeatedWordCount[Value[i]] = value + 1;
                }
                else
                {
                    RepeatedWordCount.Add(Value[i], 1);  // if a string is repeated and not added in dictionary , here we are adding   
                }
            }
            Console.WriteLine();
            Console.WriteLine("------------------------------------");
            Console.WriteLine("Repeated words and counts");
            foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)
            {
                Console.WriteLine(kvp.Key + " Counts are " + kvp.Value);  // Print the Repeated word and its count  
            }

解决方案

It looks you are using the wrong separator, namely ' ' (blank) to split the string in the text. You should use the ':' (colon).


try

string[] lines = File.ReadAllLines(@"D:\TextFile1.txt");
Dictionary<string, string> dictTrade = new Dictionary<string, string>();
List<Dictionary<string, string>> lstDictAllocations = new List<Dictionary<string, string>>();
int i = 0;
foreach (string line in lines)
{

    string _line = line.Trim();
    if (_line.Length == 0)
        break;
    i++;
    string[] splitter = _line.Split(':');
    string key = splitter[0];
    string value = splitter.Length == 2 ? splitter[1] : string.Join(":", splitter.Skip(1));
    dictTrade.Add(key.Trim(), value.Trim());
}


var linesAlloc = string.Join("\n", lines.Skip(i)).Split(new string[] { "Allocation:-" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in linesAlloc)
{
    string _line = line.Trim();
    if (_line.Length != 0)
    {
        Dictionary<string, string> dictAllocation = new Dictionary<string, string>();
        var innerLines = _line.Split('\n');
        foreach (string inner in innerLines)
        {

            string[] splitter = inner.Split(':');
            string key = splitter[0];
            string value = splitter.Length == 2 ? splitter[1] : string.Join(":", splitter.Skip(1));
            dictAllocation.Add(key.Trim(), value.Trim());

        }
        lstDictAllocations.Add(dictAllocation);
    }

}


Note: Take care of the formatting into a table and writing to text file.


这篇关于如何从c#.net中的STRING PARSE DATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆