嘗試獲取List中字符串的索引位置,但它始終返回零(0)作爲索引整數變量。獲取列表C中字符串的索引位置#
任何想法是什麼錯?我已經嘗試更改爲public int等。
public int GetLangPos(string cultureCode)
{
CsvRead csvRead = new CsvRead();
int index = 0;
foreach (string line in csvRead.headerNames)
{
if (line.StartsWith(cultureCode)) { return index; }
index++;
}
return index;
}
CsvRead類:
class CsvRead
{
public List<CsvLine> _csvLines = new List<CsvLine>(); //stores the csv lines
public List<string> headerNames = new List<string>(); //stores the headers of the csv file
public void GetValue()
{
/*
Key|Name|es-ES|fr-FR|ru-RU
web_key_001|Message|Mensaje|Message|Сообщение //row0
web_key_002|Close|Cerca|Fermer|Закрыть //row1
web_key_003|Administration|Administración|Administration|Aдминистрация //row2
web_key_004|Confirm|Confirmar|Confirmer|подтвердить //row3
web_key_005|Success|Éxito|Succès|Yспех //row4
*/
FileStream f = new FileStream(ConfigurationManager.AppSettings["TranslationsCsv"], FileMode.Open);
StreamReader streamReader = new StreamReader(f);
CsvConfiguration config = new CsvConfiguration();
config.Delimiter = "|";
CsvReader csvReader = new CsvReader(streamReader, config);
using (csvReader)
{
while (csvReader.Read())
{
headerNames = csvReader.FieldHeaders.ToList<string>();
CsvLine csvLine = new CsvLine();
for (int i = 0; i < headerNames.Count(); i++)
{
csvLine.fieldValues.Add(csvReader.GetField<string>(headerNames.ElementAt(i)));
}
_csvLines.Add(csvLine);
}
//test section start
//test section end
}
}
}
csvRead中有任何headerNames?看起來你沒有看到任何東西。 – PseudoAj
請添加簡單的數據更好理解 –
headerNames只包含5個元素。而cultureCode變量總是存在於headerNames列表中的某處。 – TingAli