freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/rosetta-code/count-occurrences-of-a-subs...

2.1 KiB

title id challengeType videoUrl localeTitle
Count occurrences of a substring 596fda99c69f779975a1b67d 5 计算子字符串的出现次数

Description

任务:

创建函数或显示内置函数,以计算字符串中子字符串的非重叠出现次数。

该函数应该有两个参数:

第一个参数是要搜索的字符串,第二个参数是要搜索的子字符串。

它应该返回一个整数计数。

匹配应产生最多数量的非重叠匹配。

通常,这实质上意味着从左到右或从右到左匹配。

Instructions

Tests

tests:
  - text: <code>countSubstring</code>是一个函数。
    testString: 'assert(typeof countSubstring === "function", "<code>countSubstring</code> is a function.");'
  - text: '<code>countSubstring(&quot;the three truths&quot;, &quot;th&quot;)</code>应该返回<code>3</code> 。'
    testString: 'assert.equal(countSubstring(testCases[0], searchString[0]), results[0], "<code>countSubstring("the three truths", "th")</code> should return <code>3</code>.");'
  - text: '<code>countSubstring(&quot;ababababab&quot;, &quot;abab&quot;)</code>应返回<code>2</code> 。'
    testString: 'assert.equal(countSubstring(testCases[1], searchString[1]), results[1], "<code>countSubstring("ababababab", "abab")</code> should return <code>2</code>.");'
  - text: '<code>countSubstring(&quot;abaabba*bbaba*bbab&quot;, &quot;a*b&quot;)</code>应返回<code>2</code> 。'
    testString: 'assert.equal(countSubstring(testCases[2], searchString[2]), results[2], "<code>countSubstring("abaabba*bbaba*bbab", "a*b")</code> should return <code>2</code>.");'

Challenge Seed

function countSubstring (str, subStr) {
  // Good luck!
  return true;
}

After Test

console.info('after the test');

Solution

// solution required