{"id":5312,"date":"2023-01-05T12:25:54","date_gmt":"2023-01-05T04:25:54","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5312"},"modified":"2023-01-05T12:25:54","modified_gmt":"2023-01-05T04:25:54","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84string%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84string%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684String\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, string\"><\/p>\n<h1>TypeScript \u4e2d\u7684\u5b57\u4e32<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u6240\u6709\u7684\u529f\u80fd\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6027\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u5b57\u4e32\u3002\u5728 TypeScript \u4e2d\uff0c\u5b57\u4e32\u53ef\u4ee5\u7528\u4f86\u5132\u5b58\u6587\u5b57\u8cc7\u6599\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f86\u505a\u5b57\u4e32\u8655\u7406\uff0c\u4f8b\u5982\u62c6\u5206\u3001\u641c\u5c0b\u3001\u66ff\u63db\u7b49\u7b49\u3002<\/p>\n<h2>TypeScript \u4e2d\u7684\u5b57\u4e32\u5ba3\u544a<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>let<\/code> \u6216 <code>const<\/code> \u4f86\u5ba3\u544a\u5b57\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str1: string = \"Hello World\";\nconst str2: string = \"Hello TypeScript\";\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>let<\/code> \u548c <code>const<\/code> \u4f86\u5ba3\u544a\u5169\u500b\u5b57\u4e32\uff0c\u4e26\u4e14\u6307\u5b9a\u8b8a\u6578\u7684\u578b\u5225\u70ba <code>string<\/code>\u3002<\/p>\n<h2>TypeScript \u4e2d\u7684\u5b57\u4e32\u62c6\u5206<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u4f86\u5c07\u5b57\u4e32\u62c6\u5206\u6210\u9663\u5217\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str: string = \"Hello TypeScript\";\nlet arr: string[] = str.split(\" \");\n\nconsole.log(arr); \/\/ [\"Hello\", \"TypeScript\"]\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c07\u5b57\u4e32 <code>str<\/code> \u62c6\u5206\u6210\u9663\u5217\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u653e\u5728 <code>arr<\/code> \u8b8a\u6578\u4e2d\u3002<\/p>\n<h2>TypeScript \u4e2d\u7684\u5b57\u4e32\u641c\u5c0b<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>indexOf()<\/code> \u65b9\u6cd5\u4f86\u641c\u5c0b\u5b57\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u500b\u5b57\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str: string = \"Hello TypeScript\";\nlet index: number = str.indexOf(\"TypeScript\");\n\nconsole.log(index); \/\/ 6\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>indexOf()<\/code> \u65b9\u6cd5\u641c\u5c0b\u5b57\u4e32 <code>str<\/code> \u4e2d\u662f\u5426\u5305\u542b <code>TypeScript<\/code>\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u653e\u5728 <code>index<\/code> \u8b8a\u6578\u4e2d\u3002<\/p>\n<h2>TypeScript \u4e2d\u7684\u5b57\u4e32\u66ff\u63db<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\u4f86\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u500b\u5b57\u4e32\u66ff\u63db\u6210\u53e6\u4e00\u500b\u5b57\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str: string = \"Hello TypeScript\";\nlet newStr: string = str.replace(\"TypeScript\", \"World\");\n\nconsole.log(newStr); \/\/ \"Hello World\"\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\u5c07\u5b57\u4e32 <code>str<\/code> \u4e2d\u7684 <code>TypeScript<\/code> \u66ff\u63db\u6210 <code>World<\/code>\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u653e\u5728 <code>newStr<\/code> \u8b8a\u6578\u4e2d\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86 TypeScript \u4e2d\u7684\u5b57\u4e32\uff0c\u4e26\u4e14\u5b78\u7fd2\u4e86\u5982\u4f55\u5ba3\u544a\u3001\u62c6\u5206\u3001\u641c\u5c0b\u548c\u66ff\u63db\u5b57\u4e32\u3002TypeScript \u4e2d\u7684\u5b57\u4e32\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u8f15\u9b06\u5730\u8655\u7406\u6587\u5b57\u8cc7\u6599\uff0c\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u6613\u8b80\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u53c3\u8003\u8cc7\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/basic-types.html\" target=\"_blank\" rel=\"noopener\">TypeScript \u57fa\u672c\u578b\u5225<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-TW\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\" target=\"_blank\" rel=\"noopener\">JavaScript \u5b57\u4e32\u53c3\u8003<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2TypeScript\u4e2d\u7684String\u529f\u80fd\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528String\u51fd\u6578\u4f86\u64cd\u4f5c\u5b57\u4e32\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5efa\u7acb\u66f4\u5f37\u5927\u7684\u61c9\u7528\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":[187],"tags":[186],"class_list":["post-5312","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1nG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5312","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=5312"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5312\/revisions"}],"predecessor-version":[{"id":5313,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5312\/revisions\/5313"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}