开发者

C# insert string in multiline string

I have a multiline string (from a txt-file using ReadAllText). the string looks like this:

R;0035709310000026542510X0715;;;  
R;0035709310000045094410P1245;;;  
R;0035709310000026502910Z1153;;;

I want to put in a ";" in each line on place 22, so it looks like this:

R;00357093100000265425;10X0715;;;  
R;0035709310000045094开发者_开发技巧4;10P1245;;;  
R;00357093100000265029;10Z1153;;;

The multiline string always contain the samme amount of data but not always 3 lines - sometimes more lines.

How do I make this? Please show some code.

Thanks alot :-) Best regards Bent


Try this ...

using System.IO;
using System.Linq;

var lines = File.ReadAllLines("data.txt");
var results = lines.Select(x => x.Insert(22, ";"));


Step 1, don't use ReadAllText(). Use ReadAllLines() instead.

 string[] myLinesArray = File.ReadAllLines(...);

Step 2, replace all lines (strings) with the changed version.

for(int i = 0; i < myLinesArray.Length; i++)
    myLinesArray[i] = myLinesArray[i].Insert(22, ";");

Step 3, Use WriteAllLines()


try this

string s ="R;0035709310000026542510X0715;;;";
s = s.Insert(22,";");
Console.Write(s);

or use Regex

    string s =@"R;0035709310000026542510X0715;;;
R;0035709310000045094410P1245;;;
R;0035709310000026502910Z1153;;;";
    string resultString = Regex.Replace(s, "^.{22}", "$0;", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    Console.Write(resultString);


I think it would be better to read the source file line by line and modify the line as you go.

You could build up your new file in a StringBuilder or, if is large, write it to a new file, used to replace the source at the end.

Something like this,

using System.IO;

string tempFileName = Path.GetTempFileName();

using (StreamWriter target = File.CreateText(tempFileName))
{
    using(StreamReader source = file.OpenText("YourSourceFile.???"))
    {
        while (source.Peek() >= 0)
        {
            target.WriteLine(source.ReadLine().Insert(22, ";"));
        }
    }
}

File.Delete("YourSourceFile.???");
File.Move(tempFileName, "YourSourceFile.???");

This approach becomes is especially appropriate for large files since it avoids loading all the data into memory at once but the performance will be good for all but very large files or, I guess, if the lines were very (very) long.


As suggested, you can use the Insert method to achieve your goal.

If your file contains a lot of lines and you need to work on 1 line at a time, you might also consider reading it line by line from a TextReader.


You could go with Regex:

myString = Regex.Replace(myString, @"(^.{22})", @"\1;", RegexOptions.Multiline);

Explanation: you have 3 string arguments:

  • 1st one is the input
  • 2nd is the pattern
  • 3rd is the replacement string

In the pattern:

  • () is a capturing group: you can call it in the replacement string with \n, n being the 1-based index of the capturing group in the pattern. In this case, \1 is whatever matched "(^.{22})"
  • "^" is the beginning of a line (because we set the multiline options, otherwise it would be the beginning of the input string)
  • "." matches any character
  • {22} means you want preceeding pattern (in this case ".", any character) 22 times

So what that means is: "in any line with 22 characters or more, replace the 22 first characters by those same 22 characters plus ";"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜