【发布时间】:2021-07-14 14:00:54
【问题描述】:
我定义了这个变量:
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。一切都在低处,但阴森森的仍然没有离开大楼。
如何让替换忽略大写?
完整代码如下
<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>
【问题讨论】:
-
这不是真正的替代品,但也许它适用于这种情况?
filePath.Remove(filePath.IndexOf(string2remove, StringComparison.InvariantCultureIgnoreCase), string2remove.Length);如果这是你要找的,我可以为你写一个答案。但我不确定这是否是您要求的。 -
我无法通过您的示例输入重现您的问题。你能发一个minimal reproducible example吗?
-
string stan = filePath.ToLower().Replace(string2remove.ToLower(),"");然后在执行获取文件名操作之前打印该值。 (移动自动取款机) -
请把问题解释清楚,我开始看了,一大堆...什么都清楚...路径,slimshady,奇怪的#代码...都是什么?