最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to compare only first character of 2 strings (javascript)? - Stack Overflow

programmeradmin5浏览0评论

Trying to write a javascript function for paring only first character of 2 strings but can't make it work. Here is the code.

function pare(wordOne, wordTwo) {
    if (wordOne.substring(0) === wordTwo.substring(0))
    {
        return true;
    } else {
        return false;
    }
}
pare("house", "hell");

Trying to write a javascript function for paring only first character of 2 strings but can't make it work. Here is the code.

function pare(wordOne, wordTwo) {
    if (wordOne.substring(0) === wordTwo.substring(0))
    {
        return true;
    } else {
        return false;
    }
}
pare("house", "hell");
Share Improve this question edited Dec 5, 2024 at 16:51 Atul KS 99011 silver badges25 bronze badges asked Apr 27, 2018 at 12:29 nitringnitring 211 gold badge1 silver badge5 bronze badges 5
  • String letters -> What exactly are you trying to do? – Nikhil Aggarwal Commented Apr 27, 2018 at 12:30
  • What is your code trying to achieve? You want to pare only first chaaracter? – gurvinder372 Commented Apr 27, 2018 at 12:30
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… with .substring() you have the option to include where the substring starts and ends (if end is omitted, it will capture to the end of the string)... wordOne.substring(0,1) – Doug Commented Apr 27, 2018 at 12:33
  • Yes sorry i didn't explain it really nice...but yes i only need to pare first character. – nitring Commented Apr 27, 2018 at 12:34
  • You can also treat strings as arrays of characters. str = "hello" means that str[1] = e – TheCrzyMan Commented Apr 27, 2018 at 12:35
Add a ment  | 

3 Answers 3

Reset to default 5

Assuming you want to pare the first letter of the two strings, you can use the following code

function pare(wordOne, wordTwo) {
    return wordOne[0] === wordTwo[0];
}
pare("house", "hell");

This condenses the if/else condition, as you are just interested in whether the first letters are equal - not in how different they are. You can also use str.toUpperCase() (or) str.toLowerCase() in order to make the parison case insensitive.

As per @Josh Katofsky's suggestion, you can of course make this function more versatile by - for instance - adding a third parameter that tests the n-th letter:

function pare(wordOne, wordTwo, index) {
    return wordOne[index] === wordTwo[index];
}
pare("house", "hell", 0);

To explain why your current code doesn't work, you need to pass a second parameter to .substring as the to value. String.substring(0) just returns the whole string after the 0th character, so the entire word. Fixed example;

function pare(wordOne, wordTwo) {
    if (wordOne.substring(0, 1) === wordTwo.substring(0, 1)) {
        return true;
    }
    else 
    {
        return false;
    }
}

pare("house", "hell");

You could also just use wordOne[0] === wordTwo[0]

substring returns the part of the string between the start and end indexes, or to the end of the string.

If you want to pare only first character, use charAt

function pare(wordOne, wordTwo) {
   return wordOne.charAt(0) === wordTwo.charAt(0);
}
pare("house", "hell");

Or you can pass the index as the parameter

function pare(wordOne, wordTwo, index) {
   return wordOne.charAt(index) === wordTwo.charAt(index);
}
pare("house", "hell", 0);
发布评论

评论列表(0)

  1. 暂无评论