Home > Back-end >  String word wrap function, everybody to have a look at what is there is a problem, why always garble
String word wrap function, everybody to have a look at what is there is a problem, why always garble

Time:09-24


Function is as follows:

The Function MitiLine (Str: String; N: Integer) : String;
Var
I, j, zm, k, LNS, s, gs: Integer;
Strs: String;
The Begin
Zm:=0;
K:=0;
L:=Length (Str);

S:=0;
Gs:=0;
If L=0 Then
The begin
Result:=Str;
Exit;
The End;
If floattostr (L/N) & gt; Inttostr (trunc (L/N)) then
LNS:=trunc (L/N) + 1
The else
LNS:=trunc (L/N);
L:=Lns;


If L<1 THEN: L=1;

If L> 1 then
The begin

For I:=1 To L Do
The begin
//s=(N * (I - 1) + 1);
ZM:=0;

Do the for j:=0 to N - 1
The begin

If ByteType (Str, (N * (I - 1) + 1 + s + J))=mbSingleByte then
The begin
Zm:=zm + 1;
end;
end;
If zm mod 2=0 then
S:=0
The else
The begin
S:=1;
end;
If I=1 then
The BEGIN


Strs:=Strs + Copy (Str, N * (I - 1) + 1, N + s) + # 13 + # 10;
END
The else
Strs:=Strs + Copy (Str, N * gs, (I - 1) + 1 + N + s) + # 13 + # 10;


If s=1 then
Gs: gs +=1;

end;

STRS:=leftstr (STRS, length (STRS) - 2);

Result:=Strs;
End
The else
The begin

STRS:=leftstr (STRS, length (STRS) - 2);
Result:=Str;
end;

The End;


When the incoming Numbers, characters, characters and other characters in a line break will appear garbled, please help to check where have problem,

CodePudding user response:

To say the purpose of clear character conversion, let everybody guessing riddles for you?

CodePudding user response:

String line!!!!!!!!!!!!!! , specify a line length of the string, more than in another line,

CodePudding user response:

You this is in accordance with the fixed length line feed, you write too much trouble,
According to the length of a few Chinese characters?

CodePudding user response:

Two Chinese characters length

CodePudding user response:

First of all, you cannot directly determine, whether the interception byte single-byte, you can try to judge whether it is the first byte first,
 class function TPrinteTable. WordWrap (const Value: AnsiString. 
Var MaxLength: Integer) : AnsiString;
The begin
If MaxLength & gt; Length (Value) then
MaxLength:=Length (Value);
If System. AnsiStrings. ByteType (Value, MaxLength)=TMbcsByteType. MbLeadByte
Then
Dec (MaxLength);
Result:=System. AnsiStrings. LeftStr (Value, MaxLength);
end;

CodePudding user response:

The Function GetNChars (Str: AnsiString; Var N: Integer) : AnsiString;
//interception N before a string
The begin
If N & gt;=Length (Str) then
The begin
Result:=Str;
N:=0;
End
The else
The begin
//if the NTH character is the first half of the Chinese characters, is less a character
If ByteType (Str, N)=mbLeadByte then
N:=N - 1;
Result:=Copy (Str, 1, N);
end;
end;

The Function MitiLine (Str: AnsiString; N: Integer) : AnsiString;
Var
S: AnsiString.
M: Integer;
The begin
S:=';
While True do
The begin
M:=N;
If s="' then
S:=GetNChars (Str, m)
The else
S:=s + # 13 # 10 + GetNChars (Str, m);
If m=0 then
Break;
Str:=Copy (Str, m + 1, Length (Str) - m);
end;
Result:=s;
end;

//sample
Procedure TForm1. Btn1Click (Sender: TObject);
Var
Str: AnsiString;
N: Integer;
The begin
Str:='jump in early trading yesterday, the Shanghai and shenzhen two city both empty low opening, the Shanghai composite index to 3131.85 points, or 1.71%, in shenzhen at 10656.9 points, or 2.13%, in early shakeouts, dish in aerospace sector, the Shanghai composite index fell narrowed to 0.67%, but the inability to turn red,';
Memo1. Text:=MitiLine (Str, 25);
end;

CodePudding user response:

I don't know if the original poster is D7 or XE, but if is XE, systems have to ANSI and UNICODE encoding mechanism to distinguish
COPY function is already automatically identify the ANSI or UNICODE
The following code may provide reference to the building, XE8 tests pass, also advice: if not necessary, general don't run into byte operations

Var
I, iCutLength: Integer;
Str, sCurSymbol sLineStr: String;
The begin
ICutLength:=25;
Str:='jump in early trading yesterday, the Shanghai and shenzhen two city both empty low opening, the Shanghai composite index to 3131.85 points, or 1.71%, in shenzhen at 10656.9 points, or 2.13%, in early shakeouts, dish in aerospace sector, the Shanghai composite index fell narrowed to 0.67%, but the inability to turn red,';
SCurSymbol:=';
SLineStr:=';
For I:=1 to Length (Str) do
The begin
SCurSymbol:=Copy (Str, I, 1);
If Length (AnsiString (sLineStr + sCurSymbol)) & gt; ICutLength then
The begin
//if the length & gt; ICutLength will empty string
Memo1. Lines. The Add (sLineStr);
SLineStr:='
end;
SLineStr:=sLineStr + sCurSymbol;
end;
If sLineStr<> "' then
Memo1. Lines. The Add (sLineStr);
end;

CodePudding user response:

Chinese characters all Angle according to the length of the two so you must be a power of 2

CodePudding user response:

A Chinese character was split in half
  • Related