【问题标题】:Replace string regardless of capitals [closed]无论大写如何都替换字符串[关闭]
【发布时间】: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,奇怪的#代码...都是什么?

标签: c# string biml


【解决方案1】:

尝试使用具有 StringComparison 参数的重载。

此代码将 myNameIs 写入控制台:

        string toReplace = "slimshady";
        string original = "myNameIsSlimShady";
        string outcome = original.Replace(toReplace, "", StringComparison.OrdinalIgnoreCase);
        Console.WriteLine(outcome);   // outputs myNameIs

【讨论】:

    【解决方案2】:

    我最终使用了

    fileName=Regex.Replace(Path.GetFileNameWithoutExtension(filePath), string2remove,"", RegexOptions.IgnoreCase);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多