2012-01-17 138 views
-1

我是剛剛入門的C#初學者程序員。我有一個任務,程序需要讀取一個字符串並執行一些字符串操作。用戶界面提供了一個TextBox和以下所有選項作爲CheckBox es。用戶可以選擇任何或全部。初學者邏輯開發

  1. 刪除任何空格。
  2. 刪除任何特殊字符,如','等。
  3. 刪除任何數字。
  4. 轉換爲camelCase。

可以有更多的選項作爲字符串清理的一部分。我有一個方法中的字符串處理,它有一個裂縫,如果...其他ifs ...

我相信有一種方法。

感謝任何幫助。

感謝所有的解決方案,但我認爲我的觀點並沒有被正確地對待。 字符串處理將根據複選框的值以特定的順序完成。 用戶可能只選擇提供的一個或每個選項。如果有多個選擇,它應該像

if(RemoveSpaces.checked) 
{ 
    RemoveSpaces(string inputString); 
    // After removing spaces do the other operations 
} 
else if (RemoveSpecialChars.checked) 
{ 
    RemoveSpecialChars(string inputString); 
    // Do other processing 
} 
+3

你的問題是? – 2012-01-17 11:04:19

+0

你需要什麼幫助 - 更好的編碼結構?目前你有什麼(向我們展示代碼)? – 2012-01-17 11:05:01

+0

您可以在複選框事件上執行您的代碼。所以當他們被檢查時,它會執行代碼而不是其他所有事情。 – 2012-01-17 11:05:03

回答

2

你可以在裏面做一些類和4個函數。例如:

public static class StringOperations 
{ 
    public static string RemoveSpaces(string sourceString) 
    { 
     string convertedString = ""; 
     //some operations 
     return convertedString; 
    } 

    public static string RemoveCharacters(string sourceString, params char[] charactersToRemove) 
    { 
     string convertedString = ""; 
     //some operations 
     return convertedString; 
    } 

    public static string RemoveAnyNumbers(string sourceString) 
    { 
     string convertedString = ""; 
     //some operations 
     return convertedString; 
    } 

    public static string ConvertToCamelCase(string sourceString) 
    { 
     string convertedString = ""; 
     //some operations 
     return convertedString; 
    } 
} 

在UI你只需要調用的功能之一...

3

爲便於字符串處理,使用與string.replace

String.replace

此代碼示例也可能幫助:

string start = "a b 3 4 5.7"; 
string noSpace = start.Replace(" ", ""); 
string noDot = noSpace.Replace(".", ""); 
string noNumbers = Regex.Replace(noDot, "[0-9]", ""); 

Console.WriteLine(start); 
Console.WriteLine(noSpace); 
Console.WriteLine(noDot); 
Console.WriteLine(noNumbers); 

輸出將是一個s follow

"a b 3 4 5.7" // start 
"ab345.7" // noSpace 
"ab3457" // noDot 
"ab" // noNumbers