2017-02-23 126 views
-1

我同在ASCII表中的下一個字符返回它,例如:下一個字符的每個字符添加一個字符串

string tmp = "hello"; 
string modified = "ifmmp"; 

我試圖分裂字符串轉換成字符,和1每個字符,但它給出了一個錯誤。

+0

ç你顯示哪個錯誤你面臨 – Usman

+0

提示使用toCharArray()然後http://stackoverflow.com/questions/1026220/how-to-find-out-next-character-alphabetically然後concat –

+1

當問一個問題,你應該顯示我們你的代碼,並準確地說明錯誤。因爲我們絕對沒辦法告訴你什麼是錯誤的,因爲我們看不到你的代碼。即使你告訴我們錯誤是什麼,我們也許可以從中猜出你的代碼是什麼以及它出錯的地方。雖然這個問題不是有用的答覆(並且我們儘量不要根據請求爲人們編寫代碼)。 – Chris

回答

3

嘗試了這一點:

public string NextCharString(string str) 
{ 
    string result = ""; 
    foreach(var c in str) 
    { 
     if (c=='z') result += 'a'; 
     else if (c == 'Z') result += 'A'; 
     else result += (char)(((int)c) + 1) 
    } 
} 

編輯:我以爲加一來所有的字符是循環的,即,添加一個「Z」會給出一個「A」

+0

謝謝你的隊友,這正是我正在尋找的 – arstek

+0

似乎有某種字母推定你的包裝中的語言寫作系統。英文,也許。 –

+0

'char'可以隱式轉換爲'int',所以你可以通過'c + 1'去除一個額外的演員。 – TheLethalCoder

0

試試這個:

  string tmp = "hello"; 
      string modified = ""; 
      for (int i = 0; i < tmp.Length; i++) 
      { 
       char c = getNextChar(tmp[i]); 
       modified += c; 
      } 

     // 'modified' will be your desired output 

創建此方法:

 private static char getNextChar(char c) 
     { 

      // convert char to ascii 
      int ascii = (int)c; 
      // get the next ascii 
      int nextAscii = ascii + 1; 
      // convert ascii to char 
      char nextChar = (char)nextAscii; 
      return nextChar; 
     } 
+0

提到「ASCII」編碼在這裏有點誤導。 'char'是一個UTF-16編碼單元,它是Unicode字符集的幾種編碼之一。 –

相關問題