這是我的 txt 檔案,我需要獲取 x,y of O
####################
# #
# #
# #
# #
# #
# #
# O #
# #
####################
uj5u.com熱心網友回復:
試試這個代碼:
int yIndex = 0;
using (var fileStream = File.OpenRead("t.txt")) //t.txt your input file
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true))
{
String line;
//read line by line and yIndex is added each time
while ((line = streamReader.ReadLine()) != null)
{
for(int i=0; i< line.Length;i )
if(line[i]== 'O')
{
Console.WriteLine("X is: {0} Y is:{1}",i.ToString(),yIndex);
return;
}
yIndex ;
}
}
結果:
X 是:4 Y 是:7
uj5u.com熱心網友回復:
int counter = 0;
int inlinecounter = 0;
string line;
var text = "O";
System.IO.StreamReader file =
new System.IO.StreamReader("TextFile1.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
inlinecounter = line.IndexOf(text, 0);
break;
}
counter ;
}
Console.WriteLine("Line number: {0}", counter);
Console.WriteLine("inLine number: {0}", inlinecounter);
uj5u.com熱心網友回復:
那是該檔案中唯一的“O”嗎?
如果是這樣,您應該執行以下操作:
有一個計數器 Y - 這將表示您迭代了多少行。此計數器將從零開始,并且永遠不會被重置。
有一個計數器 X - 這將表示您在當前行中迭代了多少個字符。該計數器將從零開始,并將在每個新行中重置。
流動:
int x = 0
int y = 0
for line in lines
x = 0
for character in line
if character == 'O'
print(x,y)
x
y
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520830.html
標籤:C#文件文本
