StringComparison.OrdinalIgnoreCase
时间: 2024-04-18 13:30:53 浏览: 12
StringComparison.OrdinalIgnoreCase是一个枚举值,用于指定字符串比较的规则。它表示不区分大小写的字符串比较。
在.NET中,字符串比较可以使用String.Compare方法来执行。该方法接受两个字符串和一个StringComparison枚举值作为参数,用于指定比较规则。
当使用StringComparison.OrdinalIgnoreCase进行字符串比较时,会忽略字符串中的字符的大小写差异。这意味着比较时不会考虑字符的大小写,而只会根据字符的字母顺序进行比较。
以下是一个示例:
string str1 = "Hello";
string str2 = "hello";
int result = string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase);
在上面的示例中,使用StringComparison.OrdinalIgnoreCase进行比较,结果将返回0,表示两个字符串相等,因为在不区分大小写的比较中,"Hello"和"hello"被认为是相等的。
相关问题
优化这段代码: var surGivenName = RemoteHelper.GetNameInfo(userName); var listData = listAllUsers?.Select(c => new DtEmployee { OutEmployeeId = c.LoginId, SurName = surGivenName.SurName, GivenName = surGivenName.GivenName, Gender = c.Sex == "F" ? (int)Sex.女 : (int)Sex.男, ReservationType = (int)ReservationType.仅限本人, CredentialType=(int)CredentialType.身份证, Email = c.Email, Mobile = c.MobilePhone, IdCard=c.IDCard, WorkingState = string.Equals(c.Status, "enabled", StringComparison.OrdinalIgnoreCase) ? (int)WorkingState.在职 : (int)WorkingState.离职 }).ToList();
为了优化这段代码,可以考虑将获取 surGivenName 的操作放在循环外面,避免重复调用 RemoteHelper.GetNameInfo(userName) 方法。
可以将 surGivenName.SurName 和 surGivenName.GivenName 存储到一个临时变量中,然后在循环中使用该变量。
另外,可以使用三元运算符简化 c.Sex == "F" ? (int)Sex.女 : (int)Sex.男 的写法。
下面是优化后的代码:
var surGivenName = RemoteHelper.GetNameInfo(userName);
var surName = surGivenName.SurName;
var givenName = surGivenName.GivenName;
var listData = listAllUsers?.Select(c => new DtEmployee
{
OutEmployeeId = c.LoginId,
SurName = surName,
GivenName = givenName,
Gender = c.Sex == "F" ? (int)Sex.女 : (int)Sex.男,
ReservationType = (int)ReservationType.仅限本人,
CredentialType = (int)CredentialType.身份证,
Email = c.Email,
Mobile = c.MobilePhone,
IdCard = c.IDCard,
WorkingState = string.Equals(c.Status, "enabled", StringComparison.OrdinalIgnoreCase) ? (int)WorkingState.在职 : (int)WorkingState.离职
}).ToList();
这样,代码就避免了重复调用 RemoteHelper.GetNameInfo(userName) 方法,提高了效率。同时,使用临时变量存储 surGivenName.SurName 和 surGivenName.GivenName,使代码更简洁易读。
private static Dictionary<string, int> GenerateFieldColumnMapping(List<FieldTitleMapping> attrs, Worksheet worksheet) { var propColumnIndexDict = new Dictionary<string, int>(); try { var importExcelTitleRow = worksheet.Cells.Rows[0]; if (importExcelTitleRow[0].IsMerged) { importExcelTitleRow = worksheet.Cells.Rows[1]; } var titleRowEnumerator = attrs.GetEnumerator(); var rowEnumerator = importExcelTitleRow.GetEnumerator(); while (titleRowEnumerator.MoveNext()) { var currentConfigField = titleRowEnumerator.Current; bool isGetMapperSuccess = false; var columIndex = 0; while (rowEnumerator.MoveNext()) { var currentCell = rowEnumerator.Current as Cell; if (!string.IsNullOrEmpty(currentCell.StringValue)) { var curTitleI18nName = currentConfigField.FieldAttribute.TitleKey.Trim(); if (curTitleI18nName.Equals(RemoveBadgeOfTitle(currentCell.StringValue), StringComparison.OrdinalIgnoreCase)) { propColumnIndexDict.Add(currentConfigField.FieldName, columIndex); isGetMapperSuccess = true; break; } } columIndex++; } if (!isGetMapperSuccess) { //propColumnIndexDict.Clear(); //return propColumnIndexDict; } rowEnumerator.Reset(); } } catch (Exception ex) { propColumnIndexDict.Clear(); } return propColumnIndexDict; } worksheet使用的是 Aspose.Cells,优化这段代码
首先,可以将 `importExcelTitleRow` 的判断和赋值提取到一个方法中,避免重复代码:
```
private static Row GetImportExcelTitleRow(Worksheet worksheet)
{
var importExcelTitleRow = worksheet.Cells.Rows[0];
if (importExcelTitleRow[0].IsMerged)
{
importExcelTitleRow = worksheet.Cells.Rows[1];
}
return importExcelTitleRow;
}
```
接着,可以将 `titleRowEnumerator` 和 `rowEnumerator` 的初始化提取到 `while` 循环外部,避免每次循环都创建新的实例:
```
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).GetEnumerator();
while (titleRowEnumerator.MoveNext())
{
var currentConfigField = titleRowEnumerator.Current;
bool isGetMapperSuccess = false;
var columIndex = 0;
while (rowEnumerator.MoveNext())
{
var currentCell = rowEnumerator.Current as Cell;
if (!string.IsNullOrEmpty(currentCell.StringValue))
{
var curTitleI18nName = currentConfigField.FieldAttribute.TitleKey.Trim();
if (curTitleI18nName.Equals(RemoveBadgeOfTitle(currentCell.StringValue), StringComparison.OrdinalIgnoreCase))
{
propColumnIndexDict.Add(currentConfigField.FieldName, columIndex);
isGetMapperSuccess = true;
break;
}
}
columIndex++;
}
if (!isGetMapperSuccess)
{
//propColumnIndexDict.Clear();
//return propColumnIndexDict;
}
rowEnumerator.Reset();
}
```
最后,可以将 `columIndex` 的自增放到 `if` 判断的外部,避免不必要的计算:
```
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).GetEnumerator();
while (titleRowEnumerator.MoveNext())
{
var currentConfigField = titleRowEnumerator.Current;
bool isGetMapperSuccess = false;
var columIndex = 0;
while (rowEnumerator.MoveNext())
{
var currentCell = rowEnumerator.Current as Cell;
if (!string.IsNullOrEmpty(currentCell.StringValue))
{
var curTitleI18nName = currentConfigField.FieldAttribute.TitleKey.Trim();
if (curTitleI18nName.Equals(RemoveBadgeOfTitle(currentCell.StringValue), StringComparison.OrdinalIgnoreCase))
{
propColumnIndexDict.Add(currentConfigField.FieldName, columIndex);
isGetMapperSuccess = true;
break;
}
}
else
{
columIndex++;
}
}
if (!isGetMapperSuccess)
{
//propColumnIndexDict.Clear();
//return propColumnIndexDict;
}
rowEnumerator.Reset();
}
```
综上所述,优化后的代码如下:
```
private static Dictionary<string, int> GenerateFieldColumnMapping(List<FieldTitleMapping> attrs, Worksheet worksheet)
{
var propColumnIndexDict = new Dictionary<string, int>();
try
{
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).GetEnumerator();
while (titleRowEnumerator.MoveNext())
{
var currentConfigField = titleRowEnumerator.Current;
bool isGetMapperSuccess = false;
var columIndex = 0;
while (rowEnumerator.MoveNext())
{
var currentCell = rowEnumerator.Current as Cell;
if (!string.IsNullOrEmpty(currentCell.StringValue))
{
var curTitleI18nName = currentConfigField.FieldAttribute.TitleKey.Trim();
if (curTitleI18nName.Equals(RemoveBadgeOfTitle(currentCell.StringValue), StringComparison.OrdinalIgnoreCase))
{
propColumnIndexDict.Add(currentConfigField.FieldName, columIndex);
isGetMapperSuccess = true;
break;
}
}
else
{
columIndex++;
}
}
if (!isGetMapperSuccess)
{
//propColumnIndexDict.Clear();
//return propColumnIndexDict;
}
rowEnumerator.Reset();
}
}
catch (Exception ex)
{
propColumnIndexDict.Clear();
}
return propColumnIndexDict;
}
private static Row GetImportExcelTitleRow(Worksheet worksheet)
{
var importExcelTitleRow = worksheet.Cells.Rows[0];
if (importExcelTitleRow[0].IsMerged)
{
importExcelTitleRow = worksheet.Cells.Rows[1];
}
return importExcelTitleRow;
}
```