声明用于存储文件中行的列位置的属性
Declare properties for storing column position of a line in a file
假设我有一个文本文件和这样一行:
This is an example line within a file.
我需要做的是根据前缀列位置和输入 expectedString 修改此行。
例如:
当我想修改上面那行的 "example" 文本时:
我将从该行的位置 11 开始作为输入,并取 7 个字符。
它会是这样的:
TestMethod1()
{
int posStart = 11;
int posEnd = 17;
ModifyLine(line number, posStart, posEnd, expectedString)
}
我可能有很多类似的方法,唯一不同的是posStart和posEnd。我想将其更改为这样的较短版本:
TestMethod1()
{
ModifyLine(line number, examplePosStart, examplePosEnd, stringExpected)
}
+examplePosStart,examplePosEnd 将在不在同一文件中的某个地方声明。
class TextPosition
{
public constant int example1PosStart = 11;
public constant int example1PosEnd = 17;
public constant int example2PosStart = 18;
public constant int example2PosStart = 25;
}
我想知道是否还有其他更优化的方法来像上面那样在一个地方声明所有 posStarts、posEnds?
你可以使用数组
public class TextPosition
{
public int StartPos { get; set; }
public int EndPos { get; set; }
public static readonly TextPosition[] Positions = new[] {
new TextPosition { StartPos = 11, EndPos = 17 },
new TextPosition { StartPos = 18, EndPos = 25 }
}
}
假设我有一个文本文件和这样一行:
This is an example line within a file.
我需要做的是根据前缀列位置和输入 expectedString 修改此行。
例如: 当我想修改上面那行的 "example" 文本时: 我将从该行的位置 11 开始作为输入,并取 7 个字符。 它会是这样的:
TestMethod1()
{
int posStart = 11;
int posEnd = 17;
ModifyLine(line number, posStart, posEnd, expectedString)
}
我可能有很多类似的方法,唯一不同的是posStart和posEnd。我想将其更改为这样的较短版本:
TestMethod1()
{
ModifyLine(line number, examplePosStart, examplePosEnd, stringExpected)
}
+examplePosStart,examplePosEnd 将在不在同一文件中的某个地方声明。
class TextPosition
{
public constant int example1PosStart = 11;
public constant int example1PosEnd = 17;
public constant int example2PosStart = 18;
public constant int example2PosStart = 25;
}
我想知道是否还有其他更优化的方法来像上面那样在一个地方声明所有 posStarts、posEnds?
你可以使用数组
public class TextPosition
{
public int StartPos { get; set; }
public int EndPos { get; set; }
public static readonly TextPosition[] Positions = new[] {
new TextPosition { StartPos = 11, EndPos = 17 },
new TextPosition { StartPos = 18, EndPos = 25 }
}
}