字符串处理小结

字符串拷贝


字符串格式化

_tprintf只是输出到Console窗口,如:

[cpp]  view plain copy
  1. double num1, num2;  
  2. _tprintf(_T("%lf / %lf = ?\n"),num1,num2);  
_stprintf是输出到指定的字符串变量,如:

[cpp]  view plain cop
  1. TCHAR title[50];  
  2. _stprintf(title,_T("标题"));  
  3. TCHAR command[50];  
  4. _stprintf(command,_T("%s %lf / %lf"),_T("两个数为:"),num1,num2);  

从字符串获取输入

_stscanf( _strTemp, _T("%d,%d,%d,%d,%d,%d"),  &i,&j,&k,&l,&m,&n );


字符串分割

// crt_strtok.c
/* In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

int main( void )
{
   printf( "Tokens:\n" );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}
Output
Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens


The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 beNULL.

For example:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
    result = strtok( NULL, delims );

    

The above code will display the following output:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值