strtok字符串分割函数

本文介绍C语言中的strtok函数,该函数用于将字符串分割成多个令牌。通过实例演示如何使用strtok来处理复杂的字符串,包括如何指定分隔符以及如何在多次调用中正确地传递参数。

function
<cstring>

strtok

char * strtok ( char * str, const char * delimiters );
Split string into tokens
A sequence of calls to this function split  str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of  delimiters.

On a first call, the function expects a C string as argument for  str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character  not contained in  delimiters (which becomes the  beginning of the token). And then scans starting from this beginning of the token for the first character contained in  delimiters, which becomes the  end of the token. The scan also stops if the terminating  null character is found.

This  end of the token is automatically replaced by a null-character, and the  beginning of the token is returned by the function.

Once the terminating null character of  str is found in a call to  strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.

The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).

Parameters

str
C string to truncate.
Notice that this string is modified by being broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters
C string containing the delimiter characters.
These may vary from one call to another.

Return Value

A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}


Output:

Splitting string "- This, a sample string." into tokens:
This
a
sample
string

See also.
使用 IDEA 连接 MySQL 数据库时,如果出现 `Access denied for user 'root'@'localhost' (using password: YES)` 错误,通常涉及以下几个方面的配置或问题: 1. **MySQL 用户权限配置** MySQL 的用户权限管理较为严格,`root` 用户可能没有从本地连接的权限。可以通过以下命令检查并修改权限: ```sql -- 查看当前用户的权限 SELECT host, user FROM mysql.user; -- 授予 root 用户从 localhost 连接的权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your_password' WITH GRANT OPTION; FLUSH PRIVILEGES; ``` 如果需要允许从任意 IP 连接(不推荐用于生产环境),可以将 `localhost` 替换为 `%` [^2]。 2. **密码验证方式问题** 在 MySQL 8.0 及以上版本中,默认的身份验证插件已更改为 `caching_sha2_password`,而某些旧版本的 JDBC 驱动可能不支持该插件。可以通过以下方式解决此问题: - 修改用户的认证方式为 `mysql_native_password`: ```sql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES; ``` - 或者更新 JDBC 驱动至兼容版本,以支持新的认证方式 [^5]。 3. **检查数据库连接配置** 确保在 IDEA 中配置的数据库连接信息正确无误,包括用户名、密码、主机名和端口号。默认情况下,MySQL 使用端口 `3306`。可以在 `application.properties` 或 `application.yml` 文件中检查相关配置: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 4. **防火墙或端口占用问题** 检查系统防火墙设置是否阻止了 MySQL 的端口(通常是 `3306`)。此外,确保没有其他程序占用了该端口,导致 MySQL 无法正常监听连接请求 [^3]。 5. **MySQL 服务状态** 确认 MySQL 服务正在运行。可以通过命令行工具或操作系统的服务管理界面检查 MySQL 服务的状态。如果服务未启动,请尝试手动启动它 [^3]。 6. **JDBC 驱动版本问题** 确保使用JDBC 驱动与 MySQL 版本兼容。对于 MySQL 8.x,建议使用 `mysql-connector-java` 8.x 版本。可以在 `pom.xml` 中添加以下依赖项: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> ``` 7. **日志分析** 查看 MySQL 的错误日志文件(通常位于 `/var/log/mysql/error.log` 或类似路径),寻找具体的错误信息,这有助于进一步诊断问题 [^4]。 通过上述步骤,应该能够解决 IDEA 连接 MySQL 时遇到的 `Access denied for user 'root'@'localhost' (using password: YES)` 错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值