开发者

How can I replace each new line with a auto-incremented number?

I am lookind for a effective way that I can replace newlines with an auto-incrementing number.

eg.

this is line 1
this is line 2
this is line 3
this is line 4

to

1. this is line 1
2. this is line 2
3. this is line 3
4. this is line 4

Is looping through each line the only way? I guess thats the way I will implement it for now. Unless I find a better way here :) Just some pseudo code will do. But I am using C#开发者_运维技巧


This is probably easier if you use a shell command, for example:

nl -ba -s'. ' -w1


Just some pseudo code will do.

int lineNo=1;
for(String str:listOfString){
System.out.println(lineNo + " : " +  str);
lineNo++;
}  

Note: code provided is written in java , You can get the basic idea from that


You can use LINQ:

string[] lines = ...

string newText = string.Concat(lines.Select(
                     (line, index) => string.Format("{0}. {1}", index, line)));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜