JavaScript strrchr function

A JavaScript equivalent of PHP’s strrchr
function strrchr (haystack, needle) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // +   input by: Jason Wong (http://carrot.org/)
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strrchr("Line 1\nLine 2\nLine 3", 10).substr(1)
  // *     returns 1: 'Line 3'
  var pos = 0;

  if (typeof needle !== 'string') {
    needle = String.fromCharCode(parseInt(needle, 10));
  }
  needle = needle.charAt(0);
  pos = haystack.lastIndexOf(needle);
  if (pos === -1) {
    return false;
  }

  return haystack.substr(pos);
}

Example 1
strrchr("Line 1\nLine 2\nLine 3", 10).substr(1)

Should return
'Line 3'
Теги:
strrchr
Добавлено: 27 Июля 2018 21:45:05 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...