2017-01-27 67 views
-1

如何在複製到另一個字符串時忽略字符串中的/ * * /字符?如何在從一個字符串複製到另一個字符串時忽略字符 - c#

string str1 = "/*TODO:if*/"; 

如何忽略caracters之間/ ** /讓新路線是這樣的:

string str2 = "/**/"; 

我不允許使用任何庫函數!

+0

是否有可能,你可以有類似'「/ * TODO:/ *如果* /「'? – Amy

+0

是的,這是可能的 – Damjan25

+0

這個任務的目的是看你怎麼解決問題。我們告訴你你會做什麼似乎是不公平的。 –

回答

-1

試試這個代碼(這使用任何庫函數):

static string FormatString(string str) => 
    RemoveAfter(str, "/*") + SubstringFrom(str, "*/"); 

static int IndexOf(string str, string value) 
{ 
    for (int i = 0; i < str.Length - value.Length; i++) 
    { 
     bool found = true; 

     for (int j = 0; j < value.Length; j++) 
     { 
      if (str[i + j] != value[j]) 
      { 
       found = false; 
       break; 
      } 
     } 

     if (found) 
     { 
      return i; 
     } 
    } 

    return -1; 
} 

static int LastIndexOf(string str, string value) 
{ 
    for (int i = str.Length - value.Length; i >= 0; i--) 
    { 
     bool found = true; 

     for (int j = 0; j < value.Length; j++) 
     { 
      if (str[i + j] != value[j]) 
      { 
       found = false; 
       break; 
      } 
     } 

     if (found) 
     { 
      return i; 
     } 
    } 

    return -1; 
} 

static string SubstringFrom(string str, string value) 
{ 
    int startIndex = LastIndexOf(str, value); 

    int length = str.Length - startIndex; 
    char[] result = new char[length]; 

    for (int i = 0; i < length; i++) 
    { 
     result[i] = str[startIndex + i]; 
    } 

    return new string(result); 
} 

static string RemoveAfter(string str, string value) 
{ 
    int length = IndexOf(str, value) + value.Length; 
    char[] result = new char[length]; 

    for (int i = 0; i < length; i++) 
    { 
     result[i] = str[i]; 
    } 

    return new string(result); 
} 
1
string str2 = Regex.Replace(str1, @"/\*.*\*/", "/**/"); 

使用正則表達式,你可以捕捉的/*[anything]*/所有實例,並用你想要的文本替換它:/**/。但是,這將是非常貪婪的。如果你有字符串/*foo*/bar/*baz*/,它會吃掉所有的。

string str2 = Regex.Replace(str1, @"/\*.+?\*/", "/**/"); 

通過改變它是一個懶惰的正則表達式,字符串/**/bar/**/將被退回。

鑑於上面的編輯,這也可以通過做一個簡單的索引搜索沒有正則表達式 - 儘管它是一個貪婪的替代品。

string str2 = str1.Substring(0, str1.IndexOf("/*")) + "/*" + str1.Substring(str1.LastIndexOf("*/")); 

這只是需要的一切最後*/後的第一個/*,然後一切之前。

+0

這可能沒有使用任何庫函數? :P – Damjan25

+0

我嘗試了這個,它完美的工作,但不幸的是我沒有大聲使用任何庫函數 – Damjan25

+0

正則表達式不會與字符串工作,例如「/ * TODO:if */textToKeep/* TODO:else * /」; – BobyOneKenobi

-1

我不確定它是否合理,您不能使用庫函數,考慮到所有函數本質上都是庫函數。我認爲這裏的侷限性是,你不能引入一個尚未在新項目中導入的庫。沒關係,我們可以這樣做。 Typestring有一個Split函數,如果沒有,我們可以把它弄髒,並使用它的1995年這樣的indeces。我沒有去測試這些,但你應該很好地跟隨它們。說實話,這是一個有趣的小練習。

考慮:string str1 = "Stufftokeep/*TODO:if*/StufftokeepAgain";

string[] crazyHomework = str1.Split('/'); 
string result = string.Empty; 
foreach(string s in crazyHomework) 
{ 
    if(s.IndexOf('*') == -1) 
     result += s + " "; //added a space to keep them separate 
} 

,讓你有隻使用System功能。如果不這樣做,你可以將string改變爲的可愛array(無論如何這都是string)。

string result = string.Empty; 

bool copy = true; 
char[] array = str1.ToCharArray() 
foreach(char a in array) 
{ 
    int i = array.IndexOf[a]; 
    if(a == "/" && array.IndexOf(a) != array.Length - 1 
    && 
    (array[a + 1] == '*' || array[a -1] == '*')) 
    { 
     copy = !copy; 
    } 
    if(copy) 
     result += a.ToString(); 
} 

你必須在那一個一定的空間的問題,如果沒有在string空白,雖然。

-1

快速和骯髒的

string temp = null; 
string str1 = "this shoudl remain/*TODO:if*/*/*testing again */-and so should this"; 
int x, y; 
while ((x = str1.IndexOf("/*")) != -1) 
{ 
    if ((y = str1.IndexOf("*/")) > x) 
    { 
     temp += str1.Substring(0, x + 2) + str1.Substring(y, 2); 
     str1 = str1.Substring(y + 2); 
     continue; 
    } 
    temp += str1.Substring(y, x); 
    str1 = str1.Substring(x) 
} 
temp += str1; 
相關問題