C# 从一根绳子上剪下一根绳子

C# 从一根绳子上剪下一根绳子,c#,string,trim,C#,String,Trim,因此,从字符串中修剪某些内容的正常方法是按字符修剪 例如: string test = "Random Text"; string trimmedString = test.Trim(new Char[] { 'R', 'a', 'n'}); Console.WriteLine(trimmedString); //the output would be "dom Text" string test = "Random Text"; s

因此,从字符串中修剪某些内容的正常方法是按字符修剪

例如:

string test = "Random Text";
string trimmedString = test.Trim(new Char[] { 'R', 'a', 'n'});
Console.WriteLine(trimmedString);
//the output would be "dom Text"
string test = "Random Text";
string trimmedString = test.Trim(string "Ran");
Console.WriteLine(trimmedString);
//the output would be "dom Text"
但是,有没有一种方法可以完全从字符串中删除组合字符“Ran”呢

例如:

string test = "Random Text";
string trimmedString = test.Trim(new Char[] { 'R', 'a', 'n'});
Console.WriteLine(trimmedString);
//the output would be "dom Text"
string test = "Random Text";
string trimmedString = test.Trim(string "Ran");
Console.WriteLine(trimmedString);
//the output would be "dom Text"

现在,上面的代码给出了一个错误,但是我想知道这样的事情是否可能,谢谢

您可以将字符串转换为字符数组,如下所示:

string trimmedString = test.Trim("Ran".ToArray());
public static class TrimStringExtensions {
    public static string Trim(this string s, string toBeTrimmed){
         if(string.IsNullOrEmpty(toBeTrimmed)) return s;
         if(s.StartsWith(toBeTrimmed)){
              s = s.Substring(toBeTrimmed.Length);
         }
         if(s.EndsWith(toBeTrimmed)){
              s = s.Substring(0,s.Length - toBeTrimmed.Length);
         }
         return s;
    }
}
string test = "Random Text";
string textToTrim = "Ran";

if (test.StartsWith(textToTrim))
    test = test.Remove(0, textToTrim.Length);
if (test.EndsWith(textToTrim))
    test = test.Remove(test.Length - textToTrim.Length);
但是请注意,这种方式不会考虑精确匹配(按字符顺序),因此如果您的
测试
“aRndom Text”
,它仍然会被修剪为仅
“dom Text”

如果要通过精确匹配修剪字符串,可以使用以下代码:

string trimmedString = test.Trim("Ran".ToArray());
public static class TrimStringExtensions {
    public static string Trim(this string s, string toBeTrimmed){
         if(string.IsNullOrEmpty(toBeTrimmed)) return s;
         if(s.StartsWith(toBeTrimmed)){
              s = s.Substring(toBeTrimmed.Length);
         }
         if(s.EndsWith(toBeTrimmed)){
              s = s.Substring(0,s.Length - toBeTrimmed.Length);
         }
         return s;
    }
}
string test = "Random Text";
string textToTrim = "Ran";

if (test.StartsWith(textToTrim))
    test = test.Remove(0, textToTrim.Length);
if (test.EndsWith(textToTrim))
    test = test.Remove(test.Length - textToTrim.Length);
您还可以为此使用
Regex

public static class TrimStringExtensions {
     public static string Trim(this string s, string toBeTrimmed){
          if(string.IsNullOrEmpty(toBeTrimmed)) return s;
          var literal = Regex.Escape(toBeTrimmed);
          return Regex.Replace(s, $"^{literal}|{literal}$", "");
     }
}
使用它:

string trimmedString = test.Trim("Ran");

您可以像这样使用删除

string trimmedString = test.Trim("Ran".ToArray());
public static class TrimStringExtensions {
    public static string Trim(this string s, string toBeTrimmed){
         if(string.IsNullOrEmpty(toBeTrimmed)) return s;
         if(s.StartsWith(toBeTrimmed)){
              s = s.Substring(toBeTrimmed.Length);
         }
         if(s.EndsWith(toBeTrimmed)){
              s = s.Substring(0,s.Length - toBeTrimmed.Length);
         }
         return s;
    }
}
string test = "Random Text";
string textToTrim = "Ran";

if (test.StartsWith(textToTrim))
    test = test.Remove(0, textToTrim.Length);
if (test.EndsWith(textToTrim))
    test = test.Remove(test.Length - textToTrim.Length);

根据您的需要,我可以找到几种方法

你做什么,但稍微聪明一点 B.更换 这将删除字符串中任何位置的“Ran”

C.仅替换第一次出现的 请看

D.正则表达式 请参见

string.Replace()。。。。