2017-01-06 15 views
2

如何檢查字符串中的字符是否是JS中的特定字符?目前我的代碼檢查字符串的每個字母,然後通過一個巨大的if/else語句來檢查它是哪一個字母,我想知道是否有更高效的方法來做到這一點?如何檢查字符串中的字符是否是JavaScript中的特定字符?

var string = "hello"
我想它來測試字母的所有五個,看看它是字母,並有它的基礎上它是什麼字母做一些事情,因此,如果第一個字母是h,然後運行一些代碼,如果第一個字母是a,則不做任何事,並跳到下一個字母來檢查。

+2

顯示一些代碼,請。 –

+1

你的問題中* specific *的定義是什麼? – trincot

+2

恭喜13歲的程序員! - 這一點令人印象深刻。你能澄清這個問題嗎?你想在字符串中找到什麼? – ochi

回答

1

有很多方法可以做到這一點,例如,你可以有一系列的if-else語句或switch語句,我會提出一個不同的雖然選項:

var str = 'hello', 
     actions = { // Define actions (function to call) you want for specific characters 
      h: function() { 
       // Do something if character was 'h' 
       console.log('h'); 
      }, 
      l: function() { 
       // Do something if character was 'l' 
       console.log('l'); 
      }, 
      o: function() { 
       // Do something if character was 'o' 
       console.log('o'); 
      } 
     }; 

for (var i = 0; i < str.length; i++) { 
    if (actions[str[i]]) { // If there is an action/function defined for the current character then call the function 
     actions[str[i]](); 
    } 
} 

這個你不必「知道」你是目前在循環什麼角色,只要東西應該發生吧。

以供參考,實現與if-else語句相同的事情:

var str = 'hello'; 
for (var i = 0; i < str.length; i++) { 
    if (str[i] === 'h') { 
     // Do something if character was 'h' 
     console.log('h'); 
    } 
    else if (str[i] === 'l') { 
     // Do something if character was 'l' 
     console.log('l'); 
    } 
    else if (str[i] === 'o') { 
     // Do something if character was 'o' 
     console.log('o'); 
    } 
} 

並與switch語句:

var str = 'hello'; 
for (var i = 0; i < str.length; i++) { 
    switch (str[i]) { 
     case 'h': 
      // Do something if character was 'h' 
      console.log('h'); 
      break; 
     case 'l': 
      // Do something if character was 'l' 
      console.log('l'); 
      break; 
     case 'o': 
      // Do something if character was 'o' 
      console.log('o'); 
      break; 
    } 
} 
+1

只是做了類似的東西(對象數組),但不確定OP的上下文和效率:https://jsfiddle.net/trt9rpez/ :) – sinisake

1

與JS

檢查實施例,如果一個字串 「世界」 包括:

var str = "Hello world, welcome to the universe."; 
var n = str.includes("world"); 

n的結果將是:

true 

這同樣適用於在單個字符單詞

積分:http://www.w3schools.com/jsref/jsref_includes.asp

+0

對我來說,它不會似乎你的回答適用於被問到的問題,儘管OP的要求並不清楚。 –

+0

用戶編輯了這個問題,並根據新的要求,你是正確的艾倫,這不是他正在尋找的答案 – SergioAMG

+0

如果我正確地理解你,你想使用的東西_other than_一系列的if ... else if。 ..聲明來做到這一點。你真的不得不給我們更多的去這裏。例如,發佈你現有的代碼是什麼樣的 - 不管它可能是醜陋的。如果它顯示出你打算完成的任務,那麼他將能夠更好地指導你。正如其他人所說的,祝賀你是一名13歲的程序員!保持。這很值得。 –

0
function do_something(str) 
{ 
    switch (str.substr(0,1).tolower()) { 
    case 'h': 
    // call function something_else with the remainder of the string 
    something_else(str.substr(1,str.length)); 
    break; 
    case 'z': 
    another_thing(); 
    break; 
    default: 
    // no need to explicitly add a case for 'h' - its handled here 
    break; 
    } 
} 

switch是一個多路分支。還有其他的方法來切分字符串。在實踐中,使用case語句與使用if...else if....else if的序列之間的性能差異不大,但確實提高了可讀性。

某些語言還提供了一些構造,您可以在運行時定義要調用的例程。這也可能與JavaScript ,但使它很容易犯錯誤,很難調試/測試/他們。以下是作爲示例編程:

function fna() 
{ 
    ... 
} 
function fnb() 
{ 
    ... 
} 
... 
function fnz() 
{ 
    ... 
} 

var fn_to_call='fn' + str.substr(0,1).tolower() + '();'; 
eval(fn_to_call); 
相關問題