在字符串中插入换行符遇到的麻烦(100)

S

sd523

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序是这样的有一串字符串,里面有中文,英文,数字,我想字符串按要求换行比如我里面字符串长度大概100,我用length()计算出字符串的长度,用insert()在字符串长度为40的地方后插入换行符,这样每行字符串长度为40.但现在出现问题了:我是指定在长度为40的地方后插入换行符的,汉字是占个两个长度,如果字符串长度在40,41的地方是一个汉字,那这样的话我就将汉字拆了,就显示不出来这个汉字了.该怎么办,还有其它的方法使我保证换行,有不能破坏字符串里面的汉字请各位高手指点,不甚感激
 
D

del520

Unregistered / Unconfirmed
GUEST, unregistred user!
你要做一个检测,检测那个位于41的那个字符是不是普通字符,这个很好办,如果不是你就需要后移一位。可以把字符转换成ASCII码来进行检查。Ord(Copy(s,40,1))
 
S

sd523

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢一楼,Ord(Copy(s,40,1)) 代码是什么意思啊[:)]
 
Z

znxia

Unregistered / Unconfirmed
GUEST, unregistred user!
本意是取得第40个字节的asc码,同 ord(S[40]),但由于Copy返回String类型,而ord后面要求的参数是char,因此编译会无法通过。看到你的回答,真的无语了。。。。
 
Z

znxia

Unregistered / Unconfirmed
GUEST, unregistred user!
IsDBCSLeadByte 这个 API函数是用来判断某字节是否在双字节字符集(例如汉字)的前导字节集中(GB 2312-80 汉字编码中的第一个字节范围 0xA1-0xFe)。或者简单地说,如果IsDBCSLeadByte(S[40]) 返回值为true,则说明第40位和第41位为一个汉字。注意:S是String类型的,不是wideString类型的。
 
S

sd523

Unregistered / Unconfirmed
GUEST, unregistred user!
那谢谢二楼了,主要认为一楼的比较简单
 
H

hhjjhhjj

Unregistered / Unconfirmed
GUEST, unregistred user!
WideString
 
L

liuls

Unregistered / Unconfirmed
GUEST, unregistred user!
// MapInfo 图层名截短函数function InterceptLayerName(const ALayerName: string
const ALen: Byte = 20): string;var iLen: Integer
iFrontPos, iBackPos: Integer
iByteType: TMbcsByteType;begin iLen := Length(ALayerName)
if iLen > ALen then begin iFrontPos := ALen div 2
// 非单字节字符, 保证不要截到多字节字符的中间, 否则产生乱码 iByteType := ByteType(ALayerName, iFrontPos)
while not (iByteType in [mbTrailByte, mbSingleByte]) do begin Inc(iFrontPos)
iByteType := ByteType(ALayerName, iFrontPos)
end
iBackPos := iLen - (ALen div 2)
iByteType := ByteType(ALayerName, iBackPos)
while not (iByteType in [mbLeadByte, mbSingleByte]) do begin Dec(iBackPos)
iByteType := ByteType(ALayerName, iBackPos)
end
Result := Copy(ALayerName, 1, iFrontPos) + Copy(ALayerName, iBackPos, iLen - iBackPos + 1)
end else Result := ALayerName;end;
 
顶部