{"id":5617,"date":"2023-01-06T10:52:00","date_gmt":"2023-01-06T02:52:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5617"},"modified":"2023-01-06T10:52:00","modified_gmt":"2023-01-06T02:52:00","slug":"1-%e4%ba%86%e8%a7%a3react-js%e4%b8%adcountsubstrings%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/1-%e4%ba%86%e8%a7%a3react-js%e4%b8%adcountsubstrings%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd\/","title":{"rendered":"1.\u4e86\u89e3React.JS\u4e2dcountSubstrings\u51fd\u6578\u7684\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.JS, countSubstrings\"><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 React.JS \u6c42\u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u6b21\u6578(countSubstrings)<\/h1>\n<p>React.JS \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u6c42\u51fa\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\u3002<\/p>\n<h2>\u4f7f\u7528 React.JS \u6c42\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u6b21\u6578<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd React.JS\uff0c\u53ef\u4ee5\u4f7f\u7528 npm \u6216 yarn \u4f86\u5b89\u88dd\u3002<\/p>\n<pre class=\"brush: javascript\">\nnpm install react\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u65b0\u7684 React \u7d44\u4ef6\uff0c\u4e26\u5c07\u5176\u547d\u540d\u70ba CountSubstrings\u3002<\/p>\n<pre class=\"brush: javascript\">\nimport React from 'react';\n\nclass CountSubstrings extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {\n      string: '',\n      substring: '',\n      count: 0\n    };\n  }\n  render() {\n    return (\n      <div>\n        <input\n          type=\"text\"\n          value={this.state.string}\n          onChange={e => this.setState({ string: e.target.value })}\n        \/>\n        <input\n          type=\"text\"\n          value={this.state.substring}\n          onChange={e => this.setState({ substring: e.target.value })}\n        \/>\n        <button onClick={this.countSubstrings}>Count Substrings<\/button>\n        {this.state.count}\n      <\/div>\n    );\n  }\n  countSubstrings = () => {\n    const { string, substring } = this.state;\n    let count = 0;\n    let startIndex = 0;\n    while (string.indexOf(substring, startIndex) !== -1) {\n      count++;\n      startIndex = string.indexOf(substring, startIndex) + 1;\n    }\n    this.setState({ count });\n  };\n}\n\nexport default CountSubstrings;\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b React \u7d44\u4ef6\uff0c\u5b83\u6709\u5169\u500b\u8f38\u5165\u6846\uff0c\u4e00\u500b\u6309\u9215\u548c\u4e00\u500b\u6587\u672c\u6846\u3002\u8f38\u5165\u6846\u7528\u65bc\u8f38\u5165\u5b57\u7b26\u4e32\u548c\u5b50\u5b57\u7b26\u4e32\uff0c\u6309\u9215\u7528\u65bc\u8a08\u7b97\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u6587\u672c\u6846\u7528\u65bc\u986f\u793a\u7d50\u679c\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5be6\u73fe countSubstrings \u65b9\u6cd5\uff0c\u5b83\u5c07\u8a08\u7b97\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\u3002\u6211\u5011\u4f7f\u7528 indexOf \u65b9\u6cd5\u4f86\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\uff0c\u4e26\u4f7f\u7528\u4e00\u500b\u8a08\u6578\u5668\u4f86\u8a08\u7b97\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u5c07 CountSubstrings \u7d44\u4ef6\u532f\u51fa\uff0c\u4ee5\u4fbf\u5728\u5176\u4ed6\u7d44\u4ef6\u4e2d\u4f7f\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u6c42\u51fa\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\u3002\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b React \u7d44\u4ef6\uff0c\u5b83\u6709\u5169\u500b\u8f38\u5165\u6846\uff0c\u4e00\u500b\u6309\u9215\u548c\u4e00\u500b\u6587\u672c\u6846\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5be6\u73fe\u4e86 countSubstrings \u65b9\u6cd5\uff0c\u5b83\u5c07\u8a08\u7b97\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07 CountSubstrings \u7d44\u4ef6\u532f\u51fa\uff0c\u4ee5\u4fbf\u5728\u5176\u4ed6\u7d44\u4ef6\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2.\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39React.JS\u4e2dcountSubstrings\u51fd\u6578\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8a08\u7b97\u51fa\u5b57\u4e32\u4e2d\u5b50\u5b57\u7b26\u4e32\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u958b\u767c\u51fa\u66f4\u597d\u7684\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5617","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1sB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5617"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5617\/revisions"}],"predecessor-version":[{"id":5618,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5617\/revisions\/5618"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}