我知道这是一个简单的问题,但我找不到答案。
我有这个字符串:
"M1[r2][r3]"
Run Code Online (Sandbox Code Playgroud)
我只想获取"M1",我正在寻找类似strchr()
函数的对象,该函数获取字符串并在a char处停止。
使用strtok和"["作为分隔符呢?
#include <string.h> /* for strtok */
#include <stdio.h> /* for printf */
int main()
{
char str[] = "M1[r2][r3]"; // str will be modified by strtok
const char deli[] = "["; // deli could also be declared as [2] or as const char *. Take your pick...
char *token;
token = strtok(str, deli); // can also call strtok(str, "["); and not use variable deli at all
printf("%s", token); // printf("%s", str); will print the same result
/* OUTPUT: M1 */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5848 次 |
| 最近记录: |