字符串操作-2 [英] string manipulation - 2

查看:139
本文介绍了字符串操作-2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中输入了这样的文本:

I''ve got text in a file as such:

VMMAnalogs_Acquire.c                    1.26  2001_105/02  2001_105/02
    Analogs_Acquire                                  100%         100%
VMMAnalogs_Initialise.c                 1.25  2001_053/02  2001_053/02
    Analogs_InitialiseInput                          100%         100%



我想将此数据写入一个csv文件,因此需要用逗号替换某些空白区域,例如:



I want to write this data to a csv file, thus need to replace certain white space sections with a comma, as such:

VMMAnalogs_Acquire.c,1.26,2001_105/02,2001_105/02
    Analogs_Acquire,100%
VMMAnalogs_Initialise.c,1.25,2001_053/02,2001_053/02
    Analogs_InitialiseInput,100%



这是替换所有空格以开始使用的代码:



Here is code that replaces all white spaces with to get started:

string input = listTxtFileContents[line_idx];
string pattern = "\\s+";
string replacement = ",";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

listTxtFileContents[line_idx] = result;



谁能帮我吗?

我面临的问题是代码用一个逗号替换了所有空格.我不希望它代替第二行和第四行的缩进字. (Analogs_Acquire和Analogs_InitialiseInput)

P.S.请投票/否决!



Can anyone help me please?

The problem that I''m facing is that the code replaces all the spaces with a single comma. I don''t want it to replace the indentation of the 2nd and the 4th line''s, words. (Analogs_Acquire and Analogs_InitialiseInput)

P.S. please vote up/down!

推荐答案

尝试一下;

Try this;

using System;
using System.Text;
using System.Text.RegularExpressions;
namespace SomeNameSpace
{
    class Program
    {
        private static string input = "VMMAnalogs_Acquire.c                    1.26  2001_105/02  2001_105/02\n" +
                               "Analogs_Acquire                                  100%         100%\n" +
                               "VMMAnalogs_Initialise.c                 1.25  2001_053/02  2001_053/02\n" +
                               "Analogs_InitialiseInput                          100%         100%\n";
        static void Main(string[] args)
        {
            Regex regExp = new Regex("\\s+");
            StringBuilder result = new StringBuilder();
            foreach (string line in input.Split('\n'))
            {
                result.AppendLine(regExp.Replace(line, ","));
            }
            Console.WriteLine(result);
        }
    }
}




希望这会有所帮助,
弗雷德里克(Fredrik)




Hope this helps,
Fredrik


我找到了解决问题的方法,并将其发布在下面,谢谢!

I''ve found the solution to my problem and posted it down below, thanks!

/* convert list buffer to csv (comma delimited) format */
for (line_idx = 0; line_idx < listTxtFileContents.Count; line_idx++)
{
  string input = listTxtFileContents[line_idx];
  string pattern = "\\s+";
  string replacement = ",";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);
  listTxtFileContents[line_idx] = result;
  if (listTxtFileContents[line_idx].StartsWith(",") == true)
  {
    listTxtFileContents[line_idx] = listTxtFileContents[line_idx].Remove(0, 1);
    listTxtFileContents[line_idx] = "   " + listTxtFileContents[line_idx];
  }

}



P.S.投票-上/下;)



P.S. VOTE - UP/DOWN ;)


这篇关于字符串操作-2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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