What is simpliest way to get Line number from char position in String?

前端 未结 4 1451
旧巷少年郎
旧巷少年郎 2020-12-15 11:45

What is simpliest way to get Line number from char position in String in C#? (or get Position of line (first char in line) ) Is there any built-in function ? If there are n

4条回答
  •  一生所求
    2020-12-15 11:48

    Count the number of newlines in the substringed input string.

    var lineNumber = input.Substring(0, pos).Count(c=>c == '\n') + 1;
    

    edit: and do a +1 because line numbers begin at 1 :-)

提交回复
热议问题