TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The complete guide to working with strings in modern JavaScript

64 点作者 davethedevguy大约 4 年前

9 条评论

dwheeler大约 4 年前
This guide omits an IMPORTANT issue: handling characters outside the Basic Multilingual Plane (BMP). JavaScript, like some other languages, suffers from the UCS-2 curse - that is, it assumes that all characters fit inside 16 bits, even though that is no longer true.<p>For example, the cited text says: &quot;You can return the UTF-16 code for a character in a string using charCodeAt()...&quot;.<p>Not true. This only works if the UTF-16 code fits in 16 bits; if it&#x27;s more than 16 bits, charCodeAt will only return a <i>part</i> of the character.<p>There are lots of discussions about this, here&#x27;s one: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3744721&#x2F;javascript-strings-outside-of-the-bmp" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3744721&#x2F;javascript-strin...</a><p>JavaScript <i>can</i> handle characters outside the BMP, but you sometimes have to aware of the problem &amp; carefully code around it when such characters are possible.
评论 #26795031 未加载
eyelidlessness大约 4 年前
I’m surprised to see no mention of tagged literals, a much more complex version of template literals. For users they may seem ~like a function call without parentheses. But they do quite a bit more.<p>Short version: they accept an array of raw substrings and a variadic set of arguments corresponding to the runtime values provided in template positions, each positional value corresponding following the raw string preceding it.<p>That raw array is more than what it seems, it also has a getter of raw string values for the template expressions. This is what String.raw (also not mentioned) uses to treat those arguments essentially the same way an untagged template literal would.<p>It’s an odd design&#x2F;interface but it can be used to do some pretty cool stuff. For example, Zapatos[1], a type-safe SQL library for TypeScript.<p>My only complaints:<p>- I can’t think of a real reason for it to be variadic, and this makes authoring them a little more error prone. You should be able to expect one array of strings with a length N, and one array of (type checkable&#x2F;inferrable) values with a length N-1.<p>2. Likewise I can’t think of a real reason for the raw values to be bolted onto a weird array subclass. It could just as easily have been an iterable third argument.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;jawj&#x2F;zapatos" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jawj&#x2F;zapatos</a>
评论 #26817690 未加载
评论 #26817687 未加载
rav大约 4 年前
<p><pre><code> &#x2F;&#x2F; Correct: returns &#x27;1&#x27; &#x27;Résumé&#x27;.localeCompare(&#x27;RESUME&#x27;, undefined, {sensitivity: &#x27;accent&#x27;}) </code></pre> localeCompare() returns 0 if the strings are equal and -1&#x2F;+1 if they&#x27;re different. Since this section is about comparing two strings that only differ in case and accents, I would expect to see a method I could use that would consider the strings to be equal. Instead, this example just shows two ways to compare strings (=== and localeCompare) that both consider the strings to be different.
评论 #26793183 未加载
fabiospampinato大约 4 年前
I&#x27;ll add something: if a string [^1] contains essentially only ASCII [^2] characters v8 will use 1 byte per character, if that string contains _any_ character other than ASCII characters in it then it will use 2 bytes for each character in the string. Said it differently storing strings as lines may save you up to 50% of memory usage depending on your use case.<p>[^1]: It actually depends on how that string was made, if internally it still references the parent string then slicing it up into lines won&#x27;t save you any memory. I&#x27;m referring to &quot;flattened&quot; strings.<p>[^2]: I don&#x27;t remember what the exact character set is, I think it&#x27;s not exactly ASCII but close enough.
评论 #26796661 未加载
评论 #26796444 未加载
评论 #26796424 未加载
cproctor大约 4 年前
&gt; If you&#x27;re not sure [which equality operator] to use, prefer strict equality using ====.<p>Super-strict!
评论 #26792895 未加载
mgerullis大约 4 年前
&gt; let website = new String(&quot;BaseClass&quot;)<p>&gt; website.rating = &quot;great!&quot;<p>Ok that’s new to me. Can anyone point out where this could be useful, if anywhere?
评论 #26814904 未加载
评论 #26793796 未加载
评论 #26793789 未加载
8bitsrule大约 4 年前
Fine concise reference!<p>While SPLITting a string into an array is useful, JOINing array elements to create a string is also useful.<p>e.g. let x = commaSepList.split(&#x27;,&#x27;).join(&#x27;\n&#x27;)
评论 #26803785 未加载
mmabbq大约 4 年前
I think there&#x27;s a minor typo in the trimming examples:<p><pre><code> &quot; Trim Me &quot;.trim() &#x2F;&#x2F; &quot;Trim&quot; </code></pre> should be<p><pre><code> &quot; Trim Me &quot;.trim() &#x2F;&#x2F; &quot;Trim Me&quot;</code></pre>
评论 #26796705 未加载
timyim大约 4 年前
Great site. Great idea. Keep it up and add more!
评论 #26793682 未加载