爱意满满的作品展示区。
kingapi

深夜肝了一个计算字符串长度的在线工具

  •  
  •   kingapi · Jul 29, 2023 · 4751 views
    This topic created in 1195 days ago, the information mentioned may be changed or developed.
    • 平时经常用到计算一下字符串的长度,经常在浏览器 console 里面写 js 函数计算,很不方便

    • 于是自己肝了一个在线工具 stringlength.org ,希望能帮到有同样需求的老哥们。

    30 replies  •  2023-08-02 17:33:55 +08:00
    Vegetable
    Vegetable
    Jul 29, 2023
    1
    很遗憾的通知您,你复制页面的时候把 Select your language 这个位置的文本复制错了。
    airyland
    airyland
    Jul 29, 2023
    2
    不用写函数,每次都是 console 里 `String`.length
    Puteulanus
    Puteulanus
    Jul 29, 2023
    3
    很多剪贴板管理的应该顺带有带这个,像 alfred 的就会显示 words 和 chars
    serafin
    serafin
    Jul 29, 2023❤️4
    4
    我也用 chatGPT 写了一个。复制下文到 ChatGPT 获取代码。

    “用 js 写一个计算一下字符串长度的 web 页面,用 css 美化一下。”
    lhbc
    lhbc
    Jul 29, 2023
    5
    啊,难道你们的文本编辑器不会显示选中的字符串长度吗?
    dcsuibian
    dcsuibian
    Jul 29, 2023❤️1
    6
    输入“𠮷”( 0x20BB7 )后显示 2
    ericls
    ericls
    Jul 29, 2023
    7
    ericls
    ericls
    Jul 29, 2023
    8
    还有更猛的
    JeffGe
    JeffGe
    Jul 29, 2023 via Android
    9
    JavaScript str.length 计算的是 UTF-16 编码下码点的数量,如果要正确的计算 Unicode 字符的数量,需要用 [...str].length

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
    cxtrinityy
    cxtrinityy
    Jul 29, 2023 via Android❤️2
    10
    深夜果然不适合干活,你看,肝出一套 bug🌚
    zictos
    zictos
    Jul 29, 2023
    11
    js:
    "Hello, World!".length

    python:
    len("Hello, World!")

    这不是很简单吗?单独计算字符串中的中文字符数量才有必要写一写,不过也很简单。或者考虑把中文当一个字,英文一个单词或两个字符当一个字。
    hackpro
    hackpro
    Jul 29, 2023 via iPhone
    12
    @ericls #8 你这是恶意评论😄
    ericls
    ericls
    Jul 29, 2023
    13
    @hackpro 什么恶意评论,明显就是被 codec, collation 这种东西坑过 哈哈
    ByteRan
    ByteRan
    Jul 29, 2023
    14
    试试这个 这些,巨爽 👩‍👩‍👧‍👧


    https://www.emojiall.com/zh-hans/categories/B

    ByteRan
    ByteRan
    Jul 29, 2023
    15
    👪👨‍👩‍👧👨‍👩‍👦‍👦👨‍👨‍👦👨‍👨‍👧‍👦👨‍👨‍👧‍👧👩‍👩‍👧👩‍👩‍👦‍👦👩‍👩‍👧‍👦👩‍👩‍👧‍👧
    24Arise
    24Arise
    Jul 29, 2023
    16
    本来 OP 较有兴致的发了个地址想跟大家分享下喜悦,哪知道 V2 这些评论区的人才,你们这些 “刁民”
    ~
    wonderfulcxm
    wonderfulcxm
    Jul 29, 2023 via iPhone❤️1
    17
    众所周知,v2ex 是一个测试人员聚集地。
    daimubai
    daimubai
    Jul 29, 2023
    19
    不是,只开发不用的吗?

    ![]( https://i.niupic.com/images/2023/07/29/bzug.jpg)
    ShuWei
    ShuWei
    Jul 29, 2023
    20
    计算的到底是什么,字符数量么?
    hsfzxjy
    hsfzxjy
    Jul 29, 2023 via Android❤️2
    21
    楼主要区分

    number of bytes
    number of code points
    number of code units
    number of graphemes
    oldshensheep
    oldshensheep
    Jul 29, 2023
    22
    看看这个
    一些语言中 String 的坑
    https://www.v2ex.com/t/852409
    hamsterbase
    hamsterbase
    Jul 29, 2023
    23
    你试试看 👩‍👩‍👧‍👦 这个符号长度是什么
    hamsterbase
    hamsterbase
    Jul 29, 2023
    24
    https://www.npmjs.com/package/runes

    直接用这个 npm 包就行了
    oldshensheep
    oldshensheep
    Jul 29, 2023
    25
    @JeffGe [...str].length 也不对,我也被 MDN 坑过(当然是我理解的问题),这种方法对于用 ZWJ 连接的字符还是会分开算。
    xinyana
    xinyana
    Jul 29, 2023
    26
    虽然没啥用,但是 [计算长度] 这个按钮不应该有
    chenzw2
    chenzw2
    Jul 31, 2023
    27
    https://online.bqrdh.com/text/assistant

    这个文本助手不是更好用
    VforVendetta
    VforVendetta
    Jul 31, 2023 via Android
    28
    甘地
    kylebing
    kylebing
    Aug 1, 2023
    29
    可以看看 Emoji 的计算长度。
    https://blog.csdn.net/KimBing/article/details/121952287
    hangbale
    hangbale
    Aug 2, 2023
    30
    字符串处理其实挺复杂的 ICU 库是必须的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1240 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 22:17 · PVG 06:17 · LAX 14:17 · JFK 17:17
    ♥ Do have faith in what you're doing.