简体   繁体   English

在特定字符之后将字符添加到字符串中

[英]Adding a character into a string after a specific character

In order to be able to read a file in asp.net, the file path must be written in this: 1. 为了能够读取asp.net中的文件,必须以以下方式编写文件路径:1。

C:\\yung\\Desktop

returns however, the string that the fileUpload get returns is 2. 但是返回,fileUpload get返回的字符串是2。

C:\yung\Desktop

After reading the comments i have this code: 阅读评论后,我有此代码:

        string FilePath = FileUploadPublicInfo.PostedFile.FileName;
        System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
        string line = File.ReadLines(FilePath.ToString()).Skip(4).ToString();

        TextBox1.Text = line.ToString();

But now its giving this error: 但是现在它给出了这个错误:

System.Linq.Enumerable+<SkipIterator>d__30`1[System.String]

How to solve this problem? 如何解决这个问题呢?

Thank you. 谢谢。

I'm not so sure I understand the question, but I think you are looking for string.Replace : 我不确定我是否理解这个问题,但是我认为您正在寻找string.Replace

string DoubleSlash(string singleSlash)
{
    return singleSlash.Replace(@"\", @"\\");
}

The reason backslashes disappear is that C# compiler treats slashes in string literals as a special "escape" character. 反斜杠消失的原因是C#编译器将字符串文字中的斜杠视为特殊的“转义”字符。 Because of this treatment, backslash needs to be encoded as two slashes in a regular string literal. 由于这种处理,反斜杠需要在常规字符串文字中编码为两个斜杠。

C# offers two ways of inserting backslashes the way you need: C#提供了两种以所需方式插入反斜杠的方式:

  • Use verbatim literals - prefix it with "at" sign, ie @"C:\\\\yung\\\\Desktop" , or 使用逐字文字 -在其前面加上“ at”符号,即@"C:\\\\yung\\\\Desktop" ,或者
  • Double each slash - put two slashes for each slash in the result: C:\\\\\\\\yung\\\\\\\\Desktop 将每个斜杠加倍 -在结果中为每个斜杠放置两个斜杠: C:\\\\\\\\yung\\\\\\\\Desktop

Ok, i have manage to solve this problem, turns out it was not reading anything. 好的,我已经设法解决了这个问题,原来它什么也没读。

This is the code that i finally get: 这是我最终得到的代码:

This is to retrieve the File's path, using this, would give the file path will double slash, so there is not a need for Replace(@"\\",@"\\") 这是检索文件的路径,使用它,将使文件路径将双斜杠,因此不需要Replace(@“ \\”,@“ \\”)

string FilePath = FileUploadPublicInfo.PostedFile.FileName;

Then read the specified file 然后读取指定的文件

System.IO.StreamReader file = new System.IO.StreamReader(FilePath);

If you know which line you specifically want, this retrieves the 5th line 如果知道您特别想要的那一行,它将检索第5行

string line = File.ReadLines(FilePath.ToString()).Skip(4).First().ToString();

Thank you so much for your help... 非常感谢你的帮助...

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

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