zoukankan
html css js c++ java
VC中的字符串操作
//
本文内容: 1.字符串的连接; 2.字符串数组的赋值; 3.LPWSTR,BSTR中字符串(含中文)的显示;4.数据转换;
#include
<
stdlib.h
>
#include
<
stdio.h
>
#include
<
conio.h
>
#include
"
iostream.h
"
#include
"
comutil.h
"
#include
<
comdef.h
>
#pragma comment( lib,
"
comsupp.lib
"
)
#define
LEN 100
//
字符串的连接
BOOL getValue()
{
char
buffer2[
100
];
_bstr_t s,s2;
s
=
L
"
S7:[S7 connection_1]DB90,X
"
;
float
f
=
0.0f
;
char
*
p,
*
p2;
//
p=(char *)malloc(100);
p
=
new
char
[
100
];
p2
=
"
1234
"
;
strcpy(p,
"
S7:[S7 connection_1]DB90,X
"
);
//
p="S7:[S7 connection_1]DB90,X" ;
BOOL Flag
=
TRUE;
for
(
int
j1
=
0
;j1
<
68
;j1
++
)
{
sprintf( buffer2,
"
%0.1f
"
,f);
//
cout<<"buffer2="<<buffer2<<endl;
LPWSTR lps;
if
(Flag)
{
s2
=
s
+
buffer2;
//
cout<<endl<<s+buffer2<<endl;
//
cout<<endl<<s2<<endl;
lps
=
::SysAllocString(s2);
//
cout<<" lps="<<lps<<endl;
}
else
{
p2
=
buffer2;
strcpy(p,
"
S7:[S7 connection_1]DB90,X
"
);
//
char *p3="123";
//
char *p23="123";
//
int *i=NULL;
//
int j=100;
//
i=&j;
//
cout<<"i="<<i<<endl;
//
cout<<"p3="<<p3<<endl;
//
if(p3==p23)
//
//
if("aa"=="aa")
//
{
//
cout<<"true";
//
}
//
else
//
{
//
cout<<"false";
//
}
strcat(p,p2);
lps
=
_com_util::ConvertStringToBSTR(p) ;
}
//
cout<<"编号: "<<j1<<" 地址连接符: ";
wprintf(L
"
%3d. %s\n
"
, j1,lps);
f
+=
0.1f
;
}
return
true
;
}
//
应用字符串连接
int
printstr()
{
char
str[]
=
"
ABCDE
"
;
char
*
string
=
"
ABCDE
"
;
string
=
(
char
*
)malloc(LEN);
strcpy(
string
,
"
123456
"
);
cout
<<
endl;
cout
<<
"
字符串输出str[]=
"
<<
str
<<
endl;
cout
<<
"
字符串输出*string=
"
<<
string
<<
endl;
char
*
AppendStr;
AppendStr
=
"
abc
"
;
for
(
int
i
=
0
; i
<
5
; i
++
)
{
AppendStr
=
str;
//
"456";
strcat(
string
,AppendStr);
//
strcat(string, "a");
}
printf(
"
%s\n
"
,
string
);
free(
string
);
getchar();
return
1
;
}
int
main(
void
)
{
//
printstr();
//
system("PAUSE");
if
(getValue())
cout
<<
"
赋值成功
"
<<
endl;
system(
"
PAUSE
"
);
//
通过sprintf函数进行数据类型转换
char
buffer2[
200
];
char
c
=
'
1
'
;
int
i
=
35
;
long
j
=
1000
;
float
f
=
1.7320534f
;
sprintf( buffer2,
"
%c
"
,c);
sprintf( buffer2,
"
%d
"
,i);
sprintf( buffer2,
"
%d
"
,j);
//
字符串的相加
_bstr_t s,s2;
s
=
L
"
S7:[S7 connection_1]DB90,X
"
;
f
=
0.0f
;
for
(
int
j1
=
0
;j1
<
100
;j1
++
)
{
f
+=
0.1f
;
sprintf( buffer2,
"
%0.1f
"
,f);
cout
<<
"
buffer2=
"
<<
buffer2
<<
endl;
s2
=
s
+
buffer2;
//
cout<<endl<<s+buffer2<<endl;
cout
<<
endl
<<
s2
<<
endl;
BSTR bs
=
::SysAllocString(s2);
LPWSTR lps
=
::SysAllocString(s2);
cout
<<
"
bs=
"
<<
bs
<<
"
lps=
"
<<
lps
<<
endl;
wprintf(L
"
%s\n
"
, bs);
//
显示赋值结果
wprintf(L
"
%s\n
"
, lps);
}
wchar_t
*
wsir;
//
#include<comdef.h>
_bstr_t str
=
L
"
How are you,北京!
"
;
wsir
=
str;
wprintf(L
"
%s\n
"
, wsir);
//
中文字符的显示
//
说明:Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
//
原型:size_t mbstowcs( wchar_t* wcstr, const char* mbstr, size_t count );
//
参数:Parameters
//
wcstr
//
The address of a sequence of wide characters.
//
mbstr
//
The address of a sequence of multibyte characters.
//
count
//
The number of multibyte characters to convert.
char
*
a
=
"
你好,北京
"
;
wchar_t
*
b
=
new
wchar_t[
3
];
mbstowcs(b, a,
sizeof
(a)
+
1
);
puts(a);
wprintf(L
"
%s\n
"
, b);
system(
"
PAUSE
"
);
//
暂停程序的执行
LPWSTR lps
=
L
"
success OK
"
;
cout
<<
"
lps=
"
<<
lps
<<
endl;
cout
<<
"
*string2
"
<<
endl;
char
*
string2[
10
];
int
ii
=
0
;
//
字符串数组赋值
cout
<<
endl;
for
( ii
=
0
;ii
<
10
;ii
++
)
{
int
i
=
ii;
string2[i]
=
new
char
;
itoa(i,string2[i],
10
);
}
cout
<<
endl;
for
(ii
=
0
;ii
<
10
;ii
++
)
cout
<<
string2[ii]
<<
endl;
//
把双精度数转换成一个字符串。
//
char *ecvt(double value,int count,int *dec,int *sign);
//
函数 需要的头文件 兼容性
//
ecvt <stdlib.h> Win 95,Win NT
char
*
string
;
double
value;
int
dec
=
2
, sign
=
0
;
int
ndig
=
1
;
//
clrscr();
value
=
-
9.876
;
string
=
ecvt(value, ndig,
&
dec,
&
sign);
printf(
"
string = %s dec = %d sign = %d\n
"
,
string
, dec, sign);
value
=
-
123.45
;
ndig
=
15
;
string
=
ecvt(value,ndig,
&
dec,
&
sign);
printf(
"
string = %s dec = %d sign = %d\n
"
,
string
, dec, sign);
int
decimal
, sign2
=
0
;
char
*
buffer;
//
fcvt是将浮点数转换为字符串,它不属于标准的C运行时库,仅仅是VC提供的,
//
因此把其命名为_fcvt .
float
source
=
0.123456f
;
buffer
=
_fcvt( source,
2
,
&
decimal
,
&
sign2 );
//
printf("%s",buffer);
cout
<<
endl
<<
endl;
cout
<<
buffer
<<
endl;
cout
<<
source
<<
"
"
<<
decimal
<<
"
"
<<
sign2
<<
endl;
//
运行结果:source: 3.1415926535 buffer: \\\'31415927\\\' decimal: 1 sign: 0
//
decimal表示小数点的位置,sign表示符号:0为正数,1为负数
return
0
;
}
查看全文
相关阅读:
JavaScript如何获取一个元素的样式信息
Linux服务器命令行操作(小白专用)
Linux云服务器搭建node环境
C++ new和delete运算符简介
C++中free()与delete的区别
VS2017+Qt开发时打开命令调试窗口
opencv4.2版本遇到CV_MINMAX未声明标识符
CUDA 数据传输
uchar 存为8位/16位图像(QImage)
Qt Creator删除toolbar中多余的“分隔符”
原文地址:https://www.cnblogs.com/furenjun/p/vcstring.html
最新文章
Java中Split函数的问题
Cognos亲和力解释
Oracle锁定选出的结果集
Oracle中删除包含物化视图的表空间的方法
北漂,真的来了
修改Oracle数据库的字符集
面试题
Oracle自动启动(解决了Failed to autostart Oracle Net Listene和Message 1053 not found错误)
通过修改Linux服务自动启动指定应用程序
今天终于在网上安家落户了
热门文章
【转】Flash Player的垃圾内存回收机制:能否强制回收?
我的第一个完整游戏程序:俄罗斯方块
个人项目
我的github地址
原生js事件篇
pm2使用技巧
ECMAScript6
javascript 兼容总结
搭建静态网站
css3动画
Copyright © 2011-2022 走看看