替换字符串而不考虑大写
Replace string regardless of capitals
我定义了这个变量:
string string2remove ="slimshady";
我有一个字符串 filePath
,其值为 myNameIsslimshady
Path.GetFileNameWithoutExtension(filePath.Replace(string2remove,""))
给我 mynameis
但是,当 filePath 的值为 myNameIsSlimShady
Path.GetFileNameWithoutExtension(filePath.Replace(string2remove,""))
给我 myNameIsSlimShady
显然取代了对大写的关心。没问题!我将使用 ToLower()
.
将 filePath 全部小写
Path.GetFileNameWithoutExtension(filePath.ToLower().Replace(string2remove,""))
现在我得到 mynameisslimshady
。都在楼下但slimshady还没有离开大楼。
如何让替换忽略大写?
完整代码如下
<FileFormats>
<#
foreach (string filePath in myFiles)
{
bool fHasSpace = filePath.Contains(" ");
if (fHasSpace) {} else {
#>
<FlatFileFormat Name="FlatFileFormat_<#=Path.GetFileNameWithoutExtension(filePath.ToLower().Replace(string2remove,""))#>" RowDelimiter="<#=delimiter#>" ColumnNamesInFirstDataRow="true" IsUnicode="false">
<Columns>
<#
StreamReader myFile = new StreamReader(filePath);
myColumns = myFile.ReadLine().Replace(separator,"").Split(delimiter);
myFile.Close();
// to determine the column delimiter
int columnCount = 0;
string columnDelimiter = "";
foreach(string myColumn in myColumns)
{
string str_delimiter = delimiter.ToString();
columnCount++;
bool finalColumn = columnCount == myColumns.Length;
if (finalColumn)
{
columnDelimiter = "CRLF";
}
else
{ columnDelimiter = str_delimiter;
}
#>
<Column Name="<#=myColumn#>" DataType = "<#=imp_datatype#>" Length="<#=imp_length#>" Delimiter="<#=columnDelimiter#>"></Column>
<# } #>
</Columns>
</FlatFileFormat>
<#}}#>
</FileFormats>
尝试使用具有 StringComparison 参数的重载。
此代码将 myNameIs 写入控制台:
string toReplace = "slimshady";
string original = "myNameIsSlimShady";
string outcome = original.Replace(toReplace, "", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(outcome); // outputs myNameIs
我最终使用了
fileName=Regex.Replace(Path.GetFileNameWithoutExtension(filePath), string2remove,"", RegexOptions.IgnoreCase);
我定义了这个变量:
string string2remove ="slimshady";
我有一个字符串 filePath
,其值为 myNameIsslimshady
Path.GetFileNameWithoutExtension(filePath.Replace(string2remove,""))
给我 mynameis
但是,当 filePath 的值为 myNameIsSlimShady
Path.GetFileNameWithoutExtension(filePath.Replace(string2remove,""))
给我 myNameIsSlimShady
显然取代了对大写的关心。没问题!我将使用 ToLower()
.
Path.GetFileNameWithoutExtension(filePath.ToLower().Replace(string2remove,""))
现在我得到 mynameisslimshady
。都在楼下但slimshady还没有离开大楼。
如何让替换忽略大写?
完整代码如下
<FileFormats>
<#
foreach (string filePath in myFiles)
{
bool fHasSpace = filePath.Contains(" ");
if (fHasSpace) {} else {
#>
<FlatFileFormat Name="FlatFileFormat_<#=Path.GetFileNameWithoutExtension(filePath.ToLower().Replace(string2remove,""))#>" RowDelimiter="<#=delimiter#>" ColumnNamesInFirstDataRow="true" IsUnicode="false">
<Columns>
<#
StreamReader myFile = new StreamReader(filePath);
myColumns = myFile.ReadLine().Replace(separator,"").Split(delimiter);
myFile.Close();
// to determine the column delimiter
int columnCount = 0;
string columnDelimiter = "";
foreach(string myColumn in myColumns)
{
string str_delimiter = delimiter.ToString();
columnCount++;
bool finalColumn = columnCount == myColumns.Length;
if (finalColumn)
{
columnDelimiter = "CRLF";
}
else
{ columnDelimiter = str_delimiter;
}
#>
<Column Name="<#=myColumn#>" DataType = "<#=imp_datatype#>" Length="<#=imp_length#>" Delimiter="<#=columnDelimiter#>"></Column>
<# } #>
</Columns>
</FlatFileFormat>
<#}}#>
</FileFormats>
尝试使用具有 StringComparison 参数的重载。
此代码将 myNameIs 写入控制台:
string toReplace = "slimshady";
string original = "myNameIsSlimShady";
string outcome = original.Replace(toReplace, "", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(outcome); // outputs myNameIs
我最终使用了
fileName=Regex.Replace(Path.GetFileNameWithoutExtension(filePath), string2remove,"", RegexOptions.IgnoreCase);