The following two functions are separated by a string:
Function SplitsTRTOARRAY (Const Tstring, Tsplit: string): tstringlist;
// Behind the method 1, this is also the algorithm mentioned in "Delphi Super Sweeping"
VAR
t_str, t_item: wideString;
t_index, t_len: integer;
t_strlist: t_stringlist;
Begin
t_strlist: = tstringlist.create ();
t_str: = TSTRING;
T_len: = Length; TSTRING
t_index: = POS (Tsplit, T_STR); // Statement 1
IF t_index> 0 THEN
Begin
While t_index> 0 do
Begin
T_Item: = Leftstr (t_str, t_index - 1);
T_Str: = Midstr (t_str, t_index 1, t_len);
t_index: = POS (Tsplit, T_STR);
If Length (T_Item)> 0 THEN
t_strlist.add (t_item);
END;
END;
if Length (T_STR)> 0 THEN
T_Strlist.Add (t_str);
Result: = t_strlist;
END;
Function splitstring (const source, ch: string): tstringlist;
// Behind the method 2;
VAR
Temp: String;
i: integer;
Begin
Result: = TSTRINGLIST.CREATE;
TEMP: = SOURCE;
I: = POS (CH, Source);
While i <> 0 do
Begin
Result.Add (Copy (Temp, 0, I-1);
Delete (Temp, 1, I);
I: = POS (CH, TEMP);
END;
Result.Add (TEMP);
END;
It seems that there is no problem with two code, however, actually use these two paragraphs: such as:
S: = 'US A, AAB, China, DDF';
T: = SplitStrtoarray (s, ',');
// t: = splitstring (s, ',');
For i: = 0 to t.count-1 do
ShowMessage (t.strings [i]);
It will find that the method 1 cannot recognize Chinese, that is, in the string with Chinese, the method cannot be divided correctly. Method 2 can be. Where is the problem?
I tracked the program found that the two functions executed a POS () function before entering the loop, and the method 2 returned at this time is 4, the method 2 is returned, so method 1 naturally does not recognize correctly. Then the problem is coming, before their POS, the two functions have nothing to do.
Try again, the second parameter of the method 1, T_INDEX: = POS (Tsplit, T_Str); the second parameter of the POS is modified to function to the function of the value TSTRING, then the value of this sentence T_index is the correct 6, and later The segmentation is wrong.
I really can't think of it. I don't know which one has encountered a similar problem, or said that I am wrong, or Delphi's bug?