Easiest way to get line number from char to String?

Easiest way to get line number from char position to String in C #? (or get the line position (first char in the line)) Is there a built-in function? If there is no such function, this is a good solution for writing an extension, for example:

public static class StringExt { public static int LineFromPos(this String S, int Pos) { int Res = 1; for (int i = 0; i <= Pos - 1; i++) if (S[i] == '\n') Res++; return Res; } public static int PosFromLine(this String S, int Pos) { .... } } 

?

Edited: Added PosFromLine method

+6
source share
3 answers

A slight deviation from the Yang sentence without creating a new line:

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

Using Take limits input size without having to copy string data.

You should think about what the result should be if the character is a line feed, by the way ... and also if you want to treat "foo\rbar\rbaz" as three lines.

EDIT: To answer the new second part of the question, you can do something like:

 var pos = input.Select((value, index) => new { value, index }) .Where(pair => pair.value == '\n') .Select(pair => pair.index + 1) .Take(line - 1) .DefaultIfEmpty(1) // Handle line = 1 .Last(); 

I think this will work ... but I'm not sure that I simply would not have written a non-LINQ approach ...

+14
source

Count the number of lines in a line of a subscript line.

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

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

+10
source

If you intend to call a function multiple times in one long line, this class may be useful. It caches new line positions, so that later it can do O (log (line breaks in a line)) look up for GetLine and O (1) for GetOffset .

 public class LineBreakCounter { List<int> lineBreaks_ = new List<int>(); int length_; public LineBreakCounter(string text) { if (text == null) throw new ArgumentNullException(nameof(text)); length_ = text.Length; for (int i = 0; i < text.Length; i++) { if (text[i] == '\n') lineBreaks_.Add(i); else if (text[i] == '\r' && i < text.Length - 1 && text[i + 1] == '\n') lineBreaks_.Add(++i); } } public int GetLine(int offset) { if (offset < 0 || offset > length_) throw new ArgumentOutOfRangeException(nameof(offset)); var result = lineBreaks_.BinarySearch(offset); if (result < 0) return ~result; else return result; } public int Lines => lineBreaks_.Count + 1; public int GetOffset(int line) { if (line < 0 || line >= Lines) throw new ArgumentOutOfRangeException(nameof(line)); if (line == 0) return 0; return lineBreaks_[line - 1] + 1; } } 

Here is my test case:

 [TestMethod] public void LineBreakCounter_ShouldFindLineBreaks() { var text = "Hello\nWorld!\r\n"; var counter = new LineBreakCounter(text); Assert.AreEqual(0, counter.GetLine(0)); Assert.AreEqual(0, counter.GetLine(3)); Assert.AreEqual(0, counter.GetLine(5)); Assert.AreEqual(1, counter.GetLine(6)); Assert.AreEqual(1, counter.GetLine(8)); Assert.AreEqual(1, counter.GetLine(12)); Assert.AreEqual(1, counter.GetLine(13)); Assert.AreEqual(2, counter.GetLine(14)); Assert.AreEqual(3, counter.Lines); Assert.AreEqual(0, counter.GetOffset(0)); Assert.AreEqual(6, counter.GetOffset(1)); Assert.AreEqual(14, counter.GetOffset(2)); } 
+1
source

Source: https://habr.com/ru/post/896306/


All Articles