如何连接包含空值的字符串? [英] How to concatenate strings that contains has nulls?

查看:80
本文介绍了如何连接包含空值的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一种方法来连接

字符串中包含空值的字符串。


我有一个我需要的字符串另一个字符串中包含空值,以及

附加

第二个字符串,3个字符串等等到第一个字符串。


任何想法如何解决这个问题?


谢谢,

jt

解决方案
" JT" < JT **** @ hotmail.com>写道:

我似乎无法找到一种方法来连接字符串中具有空值的字符串。

我有一个字符串,我需要另一个在其中包含空值的字符串和
将第二个字符串,3个字符串等附加到第一个字符串的内容。

任何想法如何解决这个问题?




这在逻辑上是不可能的。 字符串根据定义,a />
连续的字符序列由
第一个空字符终止并包括在内。 (C99 7.1.1p1)。如果一个字符序列

包含一个非结尾的空字符,那么它不是一个字符串。


因为strcat()适用于字符串,它不能用于你正在使用的序列

(因为我确定你已经发现了。


既然你没有尾随''\0''来标记

序列的结束,你必须有其他一些方法来指定它多长时间

一旦你完成了,你可以使用memcpy()(或

memmove(),如果有可能重叠)复制周围的东西。

memcpy()所需的参数由你要复制的序列的长度确定。因为你还没告诉我们这是怎么回事
定义,我们不能提供更多细节。


当然,你仍然有相同的内存分配问题(制作

确定目标很大足以容纳连续的资源你有真正的琴弦。


-

基思汤普森:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。




" jt" < JT **** @ hotmail.com>在消息中写道

新闻:h1 ******************* @ tornado.tampabay.rr.com ...

我似乎无法找到一种方法来连接字符串中具有空值的字符串。


当然不是。根据定义,字符串不能包含

嵌入的空字符。空字符(''\ 0'')

用于表示字符串的结尾。

我有一个字符串,我需要另一个字符串,其中包含空值在它和什么
附加
第二个字符串,3个字符串等等到第一个字符串。

任何想法如何去做?




不要再将它们视为字符串了。他们只是数组

的字符。然后查找''memcpy()''和''memmove()''。

(观察文档中有关重叠

内存区域的符号)。


-Mike


>我似乎无法找到一种方法来连接

字符串。


如果它内部有空值,那就是* NOT *一个字符串。

如何找到这样的非结束或长度-string?


我有一个字符串


不,你不是。它不是一个字符串!

我需要另一个字符串


另一个* NON * -string!

其中的空值以及将第二个字符串,3个字符串等附加到第一个字符串的内容。


他们是偶然的!

任何想法如何解决这个问题?




假设:

你有多少ByteSequences ByteSequence1,ByteSequence2,...

你需要的,长度为ByteSequence1Length,ByteSequence2Length,...

并且在ByteSequence1结束时有足够的存储空间来支持其余的存储:


memcpy(ByteSequence1 + ByteSequence1Length,ByteSequence2,ByteSequence2Length );

memcpy(ByteSequence1 + ByteSequence1Length + ByteSeque nce2Length,

ByteSequence3,ByteSequence3Length);

memcpy(ByteSequence1 + ByteSequence1Length + ByteSeque nce2Length + ByteSequence3Length ,

ByteSequence4,ByteSequence4Length);

...


NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ...;

Gordon L. Burditt


I can''t seem to find a way to concatenate strings that have nulls within the
string.

I have a string that I need another string that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?

Thanks,
jt

解决方案

"jt" <jt****@hotmail.com> writes:

I can''t seem to find a way to concatenate strings that have nulls
within the string.

I have a string that I need another string that has nulls in it and
what to append the 2nd string, 3 string and so forth to the 1st
string.

Any ideas how to go about this?



It''s logically impossible. A "string" is, by definition, "a
contiguous sequence of characters terminated by and including the
first null character" (C99 7.1.1p1). If a sequence of characters
includes a null character other than at the end, it''s not a string.

Since strcat() works with strings, it can''t work with the sequences
you''re using (as I''m sure you''ve discovered.

Since you don''t have the trailing ''\0'' to mark the end of your
sequence, you have to have some other way of specifying how long it
is. Once you''ve done that, you can probably use memcpy() (or
memmove() if there''s a possibility of overlap) to copy things around.
The required arguments to memcpy() are determined by the lengths of
the sequences you want to copy. Since you haven''t told us how that''s
defined, we can''t provide more details.

Of course, you still have the same memory allocation issues (making
sure the target is big enough to hold the concatentated result) that
you have with real strings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



"jt" <jt****@hotmail.com> wrote in message
news:h1*******************@tornado.tampabay.rr.com ...

I can''t seem to find a way to concatenate strings that have nulls within
the string.
Of course not. By definition, a string cannot contain
embedded null characters. The null character (''\0'')
is used to denote the end of a string.

I have a string that I need another string that has nulls in it and what
to append
the 2nd string, 3 string and so forth to the 1st string.

Any ideas how to go about this?



Stop thinking of them as strings. They''re simply arrays
of characters. Then look up ''memcpy()'' and ''memmove()''.
(Observe the notations in the documentation about overlapping
areas of memory).

-Mike


>I can''t seem to find a way to concatenate strings that have nulls within the

string.
If it has nulls internal to it, it''s *NOT* a string.
How do you find the end or length of such a non-string?

I have a string
No, you don''t. It''s NOT a string!
that I need another string
Another *NON*-string!
that has nulls in it and what to
append
the 2nd string, 3 string and so forth to the 1st string.
They AREN''T STRINGS!!
Any ideas how to go about this?



Assuming:
You have as many ByteSequences ByteSequence1, ByteSequence2, ...
as you need, of length ByteSequence1Length, ByteSequence2Length, ...
and that there is sufficient storage at the end of ByteSequence1 to
hold the rest of them:

memcpy(ByteSequence1+ByteSequence1Length, ByteSequence2, ByteSequence2Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length,
ByteSequence3, ByteSequence3Length);
memcpy(ByteSequence1+ByteSequence1Length+ByteSeque nce2Length+ByteSequence3Length,
ByteSequence4, ByteSequence4Length);
...

NewByteSequence1Length = ByteSequence1Length + ByteSequence2Length + ... ;
Gordon L. Burditt


这篇关于如何连接包含空值的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆