字符串拼接,求助!

hft19850423 2006-10-19 04:06:42
char *str1;
char *str2;
怎样才能将二者拼接在一块?谢谢!

strcat好像要求const常量
...全文
1428 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hft19850423 2006-10-19
  • 打赏
  • 举报
回复
谢谢大家

要我写,我肯定选择string,最简便了。
可这是改别人的程序啊!无奈!
lj860603 2006-10-19
  • 打赏
  • 举报
回复
unsigned char 与 char有什么区别?
=====================
unsigned char : 无符号的char型
char : 如果省略前面的符号类型的话,那么就要看机器,有的机器默认是signed,有的默认是
unsigned.看情况。不过一般都是默认为signed.

怎么转换?用强制转换就可以了,比如:

char a = 'a';
unsigned char b = (unsigned char)a;
Jokar 2006-10-19
  • 打赏
  • 举报
回复
unsigned char 与 char有什么区别?

二者怎样互转?
谢谢!
-------------------
区别 和 unsigned int 和 int 一样~

转换嘛: 强制转换就行了
unsigned char ch1;
char ch2;
................
ch1 = (unsigned char)ch2;
hft19850423 2006-10-19
  • 打赏
  • 举报
回复
unsigned char 与 char有什么区别?

二者怎样互转?
谢谢!
Jokar 2006-10-19
  • 打赏
  • 举报
回复
to Jokar
谢谢。这我也知道,但是我是想
char *str;
str1和str2拼接在str里,而且str1,str2都是变量

----------------------
那 看 键键 就行了~呵呵
pcboyxhy 2006-10-19
  • 打赏
  • 举报
回复
strcat就可以了

STRCAT(3) Linux Programmer’s Manual STRCAT(3)

NAME
strcat, strncat - concatenate two strings

SYNOPSIS
#include <string.h>

char *strcat(char *dest, const char *src);

char *strncat(char *dest, const char *src, size_t n);

DESCRIPTION
The strcat() function appends the src string to the dest string over‐
writing the ‘\0’ character at the end of dest, and then adds a termi‐
nating ‘\0’ character. The strings may not overlap, and the dest
string must have enough space for the result.

The strncat() function is similar, except that it will use at most n
characters from src. Since the result is always terminated with ‘\0’,
at most n+1 characters are written.

RETURN VALUE
The strcat() and strncat() functions return a pointer to the resulting
string dest.

CONFORMING TO
SVID 3, POSIX, 4.3BSD, ISO 9899

SEE ALSO
bcopy(3), memccpy(3), memcpy(3), strcpy(3), strncpy(3), wcscat(3),
wcsncat(3)

GNU 1993-04-11 STRCAT(3)



char *strcat(char *dest, const char *src);

char *dest //只接受 char * 类型的
const char *src //可以传递 char *, 也可以传递const char*

const char *仅仅表示在函数里不修改 指针指向的地区的值,你可以传一般的指针过去
lj860603 2006-10-19
  • 打赏
  • 举报
回复
char *str;
str1和str2拼接在str里,而且str1,str2都是变量
======================
先把str1用strcpy复制到str中,然后用strcat把str2连接到str中就可以了。
lj860603 2006-10-19
  • 打赏
  • 举报
回复
strcat好像要求const常量
==================
没有必要规定一定要const,只是一般我们自己写个strcat的时候后面的字符串一般const修饰,因为我们不希望后面的字符串不小心被改变。
hft19850423 2006-10-19
  • 打赏
  • 举报
回复
to Jokar
谢谢。这我也知道,但是我是想
char *str;
str1和str2拼接在str里,而且str1,str2都是变量
lj860603 2006-10-19
  • 打赏
  • 举报
回复
char *str1;
char *str2;
怎样才能将二者拼接在一块?
=====================
如果是这样:
char *str1 = "hello";
char *str2 = "world";
这样是不可以连接的,因为str指向的内容存储在静态区域内容不可以修改。

如果是先给分配空间,那么就可以连接在一块,用strcat;
或者是定义成数组形式:
char str1[20] = "hello";
char str2[] = "world";
taodm 2006-10-19
  • 打赏
  • 举报
回复
用c++吧
string str1, str2;
str1 += str2;就可以了,还不怕内存越界。
Jokar 2006-10-19
  • 打赏
  • 举报
回复
既然在 C++区问的~ 那回答是:


用string 吧

string str1("hello");
string str2(" world");

cout << str1 + str2 << endl;

直接用就行了~ 多方便~
trade_create_by_buyer-CSHARP-UTF-8 │ ├app_code ┈┈┈┈┈┈┈┈┈┈类文件夹 │ │ │ ├AlipayConfig.cs┈┈┈┈┈基础配置类文件 │ │ │ ├AlipayCore.cs┈┈┈┈┈┈支付宝接口公用函数类文件 │ │ │ ├AlipayNotify.cs┈┈┈┈┈支付宝通知处理类文件 │ │ │ ├AlipaySubmit.cs┈┈┈┈┈支付宝各接口请求提交类文件 │ │ │ └MD5.cs ┈┈┈┈┈┈┈┈┈MD5类库 │ ├log┈┈┈┈┈┈┈┈┈┈┈┈┈日志文件夹 │ ├default.aspx ┈┈┈┈┈┈┈┈支付宝接口入口文件 ├default.aspx.cs┈┈┈┈┈┈┈支付宝接口入口文件 │ ├notify_url.aspx┈┈┈┈┈┈┈服务器异步通知页面文件 ├notify_url.aspx.cs ┈┈┈┈┈服务器异步通知页面文件 │ ├return_url.aspx┈┈┈┈┈┈┈页面跳转同步通知文件 ├return_url.aspx.cs ┈┈┈┈┈页面跳转同步通知文件 │ ├Web.Config ┈┈┈┈┈┈┈┈┈配置文件(集成时删除) │ └readme.txt ┈┈┈┈┈┈┈┈┈使用说明文本 ※注意※ 需要配置的文件是: alipay_config.cs default.aspx default.aspx.csreturn_url.aspx return_url.aspx.cs notify_url.aspx notify_url.aspx.cs统一命名空间为:namespace Com.Alipiay ───────── 类文件函数结构 ───────── AlipayCore.cs public static Dictionary ParaFilter(SortedDictionary dicArrayPre) 功能:除去数组中的空值和签名参数并以字母a到z的顺序排序 输入:SortedDictionary dicArrayPre 过滤前的参数组 输出:Dictionary 去掉空值与签名参数后的新签名参数组 public static string CreateLinkString(Dictionary dicArray) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 输入:Dictionary dicArray 需要拼接的数组 输出:string 拼接完成以后的字符串 public static string CreateLinkStringUrlencode(Dictionary dicArray, Encoding code) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode 输入:Dictionary dicArray 需要拼接的数组 Encoding code 字符编码 输出:string 拼接完成以后的字符串 public static void log_result(string sPath, string sWord) 功能:写日志,方便测试(看网站需求,也可以改成存入数据库) 输入:string sPath 日志的本地绝对路径 string sWord 要写入日志里的文本内容 public static string GetAbstractToMD5(Stream sFile) 功能:获取文件的md5摘要 输入:Stream sFile 文件流 输出:string MD5摘要结果 public static string GetAbstractToMD5(byte[] dataFile) 功能:获取文件的md5摘要 输入:byte[] dataFile 文件流 输出:string MD5摘要结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ MD5.cs public static string Sign(string prestr, string key, string _input_charset) 功能:签名字符串 输入:string prestr 需要签名的字符串 string key 密钥 string _input_charset 编码格式 输出:string 签名结果 public static bool Verify(string prestr, string sign, string key, string _input_charset) 功能:验证签名 输入:string prestr 需要签名的字符串 string sign 签名结果 string key 密钥 string _input_charset 编码格式 输出:string 验证结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipayNotify.cs public Notify() 功能:构造函数 从配置文件中初始化变量 public bool Verify(SortedDictionary inputPara, string notify_id, string sign) 功能:验证消息是否是支付宝发出的合法消息 输入:SortedDictionary inputPara 通知返回参数数组 string notify_id 通知验证ID string sign 支付宝生成的签名结果 输出:bool 验证结果 private string GetPreSignStr(SortedDictionary inputPara) 功能:获取待签名字符串(调试用) 输入:SortedDictionary inputPara 通知返回参数数组 输出:string 待签名字符串 private bool GetSignVeryfy(SortedDictionary inputPara, string sign) 功能:获取返回回来的待签名数组签名后结果 输入:SortedDictionary inputPara 通知返回参数数组 string sign 支付宝生成的签名结果 输出:bool 签名验证结果 private string GetResponseTxt(string notify_id) 功能:获取是否是支付宝服务器发来的请求的验证结果 输入:string notify_id 通知验证ID 输出:string 验证结果 private string Get_Http(string strUrl, int timeout) 功能:获取远程服务器ATN结果 输入:string strUrl 指定URL路径地址 int timeout 超时时间设置 输出:string 服务器ATN结果字符串 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipaySubmit.cs private static string BuildRequestMysign(Dictionary sPara) 功能:生成签名结果 输入:Dictionary sPara 要签名的数组 输出:string 签名结果字符串 private static Dictionary BuildRequestPara(SortedDictionary sParaTemp) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 输出:Dictionary 要请求的参数数组 private static string BuildRequestParaToString(SortedDictionary sParaTemp, Encoding code) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 Encoding code 字符编码 输出:string 要请求的参数数组字符串 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string strButtonValue) 功能:建立请求,以表单HTML形式构造(默认) 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个值可选:post、get string strButtonValue 确认按钮显示文字 输出:string 提交表单HTML文本 public static string BuildRequest(SortedDictionary sParaTemp) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 输出:string 支付宝处理结果 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string fileName, byte[] data, string contentType, int lengthFile) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个值可选:post、get string fileName 文件绝对路径 byte[] data 文件数据 string contentType 文件内容类型 int lengthFile 文件长度 输出:string 支付宝处理结果 public static string Query_timestamp() 功能:用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数 输出:string 时间戳字符串 ────────── 出现问题,求助方法 ────────── 如果在集成支付宝接口时,有疑问或出现问题,可使用下面的链接,提交申请。 https://b.alipay.com/support/helperApply.htm?action=supportHome 我们会有专门的技术支持人员为您处理

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧