如何每两个字插入一个字符串? [英] How to insert a string every 2 words?
本文介绍了如何每两个字插入一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有点失去了如何使用文本文件。你们有没有关于如何每两个字插入一个字符串的例子?就像一条线有相同数量的单词然后在那一行我每2个字插入一个字符串?
Hello,im kinda lost on how to work with text files. Do you guys have any examples on how to insert a string every 2 words? Like if a line has an equal number of words then in that line i insert a string every 2 words?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
namespace _U_5_6
{
class Program
{
const string CFd = "..\\..\\A.txt";
const string CFr = "..\\..\\Rezultatai.txt";
static void Main(string[] args)
{
int nr;
Read(CFd, out nr);
ReadWrite(CFd, CFr, " asdas", nr);
}
static void Read(string fv, out int nr)
{
string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
nr = 0;
int nreil;
foreach (string line in lines)
{
nreil = line.Trim().Split(' ').Count();
if ((line.Length % 2) == 0)
{
}
}
}
static void ReadWrite(string fs, string fr, string eil, int n)
{
using (var frr = File.CreateText(fr))
{
using (StreamReader reader = new StreamReader(fs,
Encoding.GetEncoding(1257)))
{
string line;
for (int i = 0; i <= n && ((line = reader.ReadLine()) != null); i++)
frr.WriteLine(line+eil);
while ((line = reader.ReadLine()) != null)
frr.WriteLine(line);
}
}
}
}
}
我尝试了什么:
我迷失了这篇文章bs我需要一些帮助
What I have tried:
Im lost with this text bs i need some help
推荐答案
首先拆分每一行,使每个单词都在一个单独的字符串中。
计算单词数。
如果它甚至使用for循环来处理它成对:
First split each line, so that each word is in a separate string.
Count the number of words.
If it's even use a for loop to process it in pairs:
for (int i = 0; i < numberOfWords; i += 2)
{
...
}
在循环内,将索引i和i + 1处的单词添加到输出中。也添加你的字符串。
Inside the loop, add the word at indexes i and i + 1 to the output. Add your string as well.
这篇关于如何每两个字插入一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文