請問C#有一個TrimEnd
或TrimStart
方法有一個string
參數嗎? 不,即使我們使用string.ToCharArray()
,如TrimEnd(str.ToCharArray());
,它表現不佳。在C#中接受字符串的TrimEnd或TrimStart方法?
例如:
string sam = "Sammy";
sam = TrimEndOrStart(sam, "my", true);
sam
應該等於"Sam"
請問C#有一個TrimEnd
或TrimStart
方法有一個string
參數嗎? 不,即使我們使用string.ToCharArray()
,如TrimEnd(str.ToCharArray());
,它表現不佳。在C#中接受字符串的TrimEnd或TrimStart方法?
例如:
string sam = "Sammy";
sam = TrimEndOrStart(sam, "my", true);
sam
應該等於"Sam"
/// <summary>
/// Does a nice trim for your string
/// </summary>
/// <param name="Subject">The subject</param>
/// <param name="Search">What to trim on the string</param>
/// <param name="TrimEnd">If set to true we trim the end</param>
/// <returns></returns>
public static string TrimEndOrStart(string Subject, string Search, bool TrimEnd)
{
if (TrimEnd)
{
if (Subject.EndsWith(Search) == false)
{
return Subject;
}
Subject = ReverseString(Subject);
Start:
string[] Array1 = Subject.Split(new string[] { ReverseString(Search) }, 2, StringSplitOptions.RemoveEmptyEntries);
if (Array1.Length == 1)
{
if (Subject.Equals(Array1[0], StringComparison.Ordinal) == false)
{
Subject = Array1[0];
goto Start;
}
}
Subject = StaticMethods.ReverseString(Subject);
return Subject;
}
if (Subject.StartsWith(Search) == false)
{
return Subject;
}
Start2:
string[] Array2 = Subject.Split(new string[] { Search }, 2, StringSplitOptions.RemoveEmptyEntries);
if (Subject.Equals(Array2[0], StringComparison.Ordinal) == false)
{
Subject = Array2[0];
goto Start2;
}
return Subject;
}
/// <summary>
/// Reverses a string array
/// </summary>
/// <param name="StrArray"></param>
/// <returns></returns>
public static string ReverseString(string Str)
{
char[] ObjectArr = Str.ToCharArray();
char[] NewObjectArr = new char[ObjectArr.Length];
int counter = 0;
for (int index = ObjectArr.Length - 1; index > -1; index--)
{
NewObjectArr[counter] = ObjectArr[index];
counter = counter + 1;
}
Str = "";
StringBuilder StrBu = new StringBuilder(1000);
for (int index = 0; index < NewObjectArr.Length; index++)
{
StrBu.Append(NewObjectArr[index]);
}
Str = StrBu.ToString();
return Str;
}
您可以使用StartWith/ENDWITH像:
static class StringTrimExtension {
public static string TrimStart(this string value, string toTrim) {
if (value.StartsWith(toTrim)) {
int startIndex = toTrim.Length;
return value.Substring(startIndex);
}
return value;
}
public static string TrimEnd(this string value, string toTrim) {
if (value.EndsWith(toTrim)) {
int startIndex = toTrim.Length;
return value.Substring(0, value.Length - startIndex);
}
return value;
}
}
static void Main(string[] args) {
string s = "Sammy";
Console.WriteLine(s);
string trimEnd = s.TrimEnd("my");
string trimStart = s.TrimStart("Sa");
Console.WriteLine(trimEnd);
Console.WriteLine(trimStart);
Console.ReadLine();
}
如果你有需要IGNORECASE可以實現並覆蓋您的修剪方法並使用覆蓋StartsWith/EndsWith方法。
編輯: 遞歸:
static class StringTrimExtension {
public static string TrimStartRecursive(this string value, string toTrim) {
string result = value;
while (TrimStart(result, toTrim, out result)) {}
return result;
}
public static string TrimEndRecursive(this string value, string toTrim) {
string result = value;
while (TrimEnd(result, toTrim, out result)) { }
return result;
}
public static string TrimStart(this string value, string toTrim) {
string result;
TrimStart(value, toTrim, out result);
return result;
}
public static string TrimEnd(this string value, string toTrim) {
string result;
TrimEnd(value, toTrim, out result);
return result;
}
public static bool TrimStart(this string value, string toTrim, out string result) {
result = value;
if (value.StartsWith(toTrim)) {
int startIndex = toTrim.Length;
result= value.Substring(startIndex);
return true;
}
return false;
}
public static bool TrimEnd(this string value, string toTrim, out string result) {
result = value;
if (value.EndsWith(toTrim)) {
int startIndex = toTrim.Length;
result = value.Substring(0, value.Length - startIndex);
return true;
}
return false;
}
}
static void Main(string[] args) {
string s = "Sammymymymy";
Console.WriteLine(s);
string trimEnd = s.TrimEndRecursive("my");
string trimStart = s.TrimStartRecursive("Sa");
Console.WriteLine(trimEnd);
Console.WriteLine(trimStart);
Console.ReadLine();
}
「不守規矩」 - 它表現良好;它的行爲如文件記錄。如果你想要的是別的東西,首先指定你想要的是什麼,然後尋找一個能夠做到這一點的函數。 – hvd
好吧,它會修剪數組中的所有字符,但不管順序如何。 – Mnyikka
事實上,這就是它的意思,所以它可以被稱爲s.TrimEnd('','\ t')',並且無論數量或順序如何,任何尾隨空格或製表符都會被刪除。這不是你想要的。然而,不清楚的是你究竟想要什麼。例如,你是否希望'MyTrimEnd(「Sammymy」,「my」)'返回'「Sam」'或者「Sammy」'? – hvd