简体   繁体   English

C#向字符串中添加一个字符

[英]Adding a character to a string in C#

I'm not new to C# and I work with strings all the time but I just can't understand why in this case I can't add a simple character to a simple string!我对 C# 并不陌生,我一直在使用字符串,但我不明白为什么在这种情况下我不能向简单的字符串添加一个简单的字符! I'm trying to read a text file into C# so that I can later insert it into a SQL server table.我正在尝试将文本文件读入 C#,以便稍后将其插入 SQL 服务器表中。 Anyway, I'm stuck on adding a single quote character to a string.无论如何,我一直坚持向字符串添加单引号字符。 My code is as follows:我的代码如下:

string text;

using (var streamReader = new StreamReader(@"The_Directory\myTextFile.txt", Encoding.UTF8))
{
    text = streamReader.ReadToEnd();
}

string[] lines = text.Split(new Char[] { '\n' });
string rearrange;

foreach (string line in lines)
{
     rearrange = "*" + line + "'";
     Console.WriteLine(rearrange);  
}

What I get as a result doesn't contain the character that I wish to add to the end of the string and only contains the character that I wanted to add to the end, at the beginning.我得到的结果不包含我希望添加到字符串末尾的字符,而只包含我想添加到末尾的字符,在开头。

The output is like this:输出是这样的:

'The first line
'The second line

I can't understand why it doesn't perform the simple string addition.我不明白为什么它不执行简单的字符串加法。 I tried to filter out any possible additional \\n character in the lines but it didn't help.我试图过滤掉行中任何可能的额外 \\n 字符,但它没有帮助。

As pointed out by @Olivier, you are splitting on \\n rather than \\r\\n .正如@Olivier 所指出的,您正在拆分\\n而不是\\r\\n That may mean there is still a \\r character ( carriage return ) at the end of every line you read.这可能意味着在您阅读的每一行的末尾仍然有一个\\r字符(回车)。 The resulting output is then:结果输出是:

*The first line\r'
*The second line\r'

which, on a typical terminal emulator, is displayed as:在典型的终端模拟器上,它显示为:

'The first line
'The second line

If you could slow down the terminal output, you'd see the * is initially there at the start of the line, before being overwritten by the ' .如果您可以减慢终端输出的速度,您会看到*最初位于行首,然后被'覆盖。


One way to get rid of the clutter is to strip off trailing whitespace:摆脱混乱的一种方法是去除尾随的空白:

rearrange = "*" + line.TrimEnd() + "'";

Note: None of this will work for files produced on a 'classic' Mac, where newline is a carriage return without linefeed.注意:这些都不适用于在“经典”Mac 上生成的文件,其中换行符是没有换行符的回车。 I'd recommend following Olivier's answer .我建议遵循奥利维尔的回答

Just try this:试试这个:

using System.IO;

string[] lines = File.ReadAllLines("The_Directory\myTextFile.txt", Encoding.UTF8);

string rearrange;

foreach (string line in lines)
{
  rearrange = "*" + line + "'";
  Console.WriteLine(rearrange);  
}

On Windows, new line is Environment.Newline = "\\r\\n" .在 Windows 上,新行是Environment.Newline = "\\r\\n"

Splitting only on '\\n' causes that the remaining '\\r' implies a go to the start of the current line and the * is replaced by a ' when outputed to the console, hence the result.仅在'\\n'上拆分会导致剩余的'\\r'暗示转到当前行的开头,并且*在输出到控制台时被'替换,因此结果。

Because the \\n historically in DOS causes the cursor only to go to down in the column, if I remember correctly, but I'm not sure, so the need of the \\r too.因为 DOS 历史上的\\n导致光标只在列中向下移动,如果我没记错的话,但我不确定,所以也需要\\r

https://fr.wikipedia.org/wiki/Carriage_Return_Line_Feed https://fr.wikipedia.org/wiki/Carriage_Return_Line_Feed

Just try the following small modification in the "Split":只需在“拆分”中尝试以下小修改:

    string text;
    using (var streamReader = new StreamReader(@"The_Directory\myTextFile.txt",Encoding.UTF8))
    {
        text = streamReader.ReadToEnd();
    }

    string[] lines = text.Replace("\r\n","\n").Split("\n");
    string rearrange;
    foreach (string line in lines)
    {
        rearrange = "*" + line + "'";
        Console.WriteLine(rearrange);
    }

Usually, a normal (Windows) UTF-8 text file has both \\r\\n on the end of each line.通常,普通 (Windows) UTF-8 文本文件在每一行的末尾都有 \\r\\n 。 If you split just by \\n, then the remaining \\r will remain and the result of displaying the \\r on the console is that the cursor jumps to the beginning of the current line, before displaying the next character.如果只用\\n 分割,那么剩余的\\r 将保留,在控制台上显示\\r 的结果是光标跳到当前行的开头,然后显示下一个字符。 Thus, it would overwrite the line with the following characters while displaying further text.因此,它会在显示更多文本时用以下字符覆盖该行。

2 Things. 2 事情。 Try string.split with stringsplitoptions.RemoveEmptyEntries.尝试 string.split 和 stringsplitoptions.RemoveEmptyEntries。 And for the StringBuilding use a StringBuilder instead of concatenating it.对于 StringBuilding 使用 StringBuilder 而不是连接它。

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

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