How do I trim leading/trailing whitespace in a standard way?

后端 未结 30 2415
一个人的身影
一个人的身影 2020-11-22 02:06

Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I\'d roll my own, but I would think this is a common problem wit

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 02:35

    Use a string library, for instance:

    Ustr *s1 = USTR1(\7, " 12345 ");
    
    ustr_sc_trim_cstr(&s1, " ");
    assert(ustr_cmp_cstr_eq(s1, "12345"));

    ...as you say this is a "common" problem, yes you need to include a #include or so and it's not included in libc but don't go inventing your own hack job storing random pointers and size_t's that way only leads to buffer overflows.

提交回复
热议问题