简体   繁体   English

如何使用C#在字符串中每个单词的末尾添加定界符

[英]how to add delimiter to end of the each word in string using c#

i have text file containing data like following. 我有包含如下数据的文本文件。

4 4 4100100001063D 1CBSME 150312 40001063ANTE LECO METERING C 3460025.57LKR 4 4 4100100001063D 1CBSME 150312 40001063ANTE LECO计量C 3460025.57LKR

i want add delimiter like following 我想像下面添加分隔符

4| 4 | 4| 4 | 4100100001063D| 4100100001063D | 1CBSME| 1CBSME | 150312| 150312 | 40001063ANTE| 40001063ANTE | LECO| 乐高| METERING| 计量| C| C | 3460025.57LKR| 3460025.57LKR |

how can i fix this problem. 我该如何解决这个问题。 help me friends 帮我啦

Try Linq : 试试Linq

var target = File
  .ReadLines(@"C:\Source.txt")
  .Select(line => line.Replace(" ", "| ") + "|");

File.WriteAllLines(@"C:\Target.txt", target);
string input = "4 4 4100100001063D 1CBSME 150312 40001063ANTE LECO METERING C 3460025.57LKR";
string pattern = "\\s+";
string replacement = "|";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement) + replacement;

This should replace all the spaces with "|" 这应将所有空格替换为“ |” and then we are appending "|" 然后我们附加“ |” to get expected output. 获得预期的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM