How to find last space in a string?

Hi,

maybe, it’s too late know, but i don’t find a useful solution to split a long text (>250 lines).

I have to fill comment lines (Text 80). It’s not that difficult, but I don’t just want so copy a srting with 80 signs (COPYSTR(TempText,Pos,MAXSTRLEN(CommentLine.Comment)); Doing it this way words are being cut. This does not look so good.

I want to find the last space in the Text 80 string blocks and copy everything left of the space. The new Position should be something like
Pos := Pos + CopiedStringLength.

Can anyone please help me?

Ciao

Gregor

this function should do it.

FindLastSpace(String : Text[100]) : Integer
For i := 1 to strlen(String) Do begin
if String[strlen(String)+1-i] = ’ ’ then
exit(strlen(String));
end;

Hi,

I’ve got a nice test form where you can test any splitting routine…

OBJECT Form 98765 String Splitting
{
OBJECT-PROPERTIES
{
Date=19.01.08;
Time=16:12:09;
Modified=Yes;
Version List=;
}
PROPERTIES
{
Width=15950;
Height=8000;
}
CONTROLS
{
{ 1140000;TextBox ;3630 ;330 ;11990;440 ;CaptionML=DEU=Input string;
SourceExpr=inputString }
{ 1140001;Label ;220 ;330 ;3300 ;440 ;ParentControl=1140000 }
{ 1140002;TextBox ;3630 ;1210 ;1700 ;440 ;CaptionML=DEU=Max. Output Length;
BlankZero=Yes;
SourceExpr=maxoutLen;
MinValue=0 }
{ 1140003;Label ;220 ;1210 ;3300 ;440 ;ParentControl=1140002 }
{ 1140004;TextBox ;3630 ;2090 ;11990;440 ;CaptionML=DEU=Output string 1;
SourceExpr=outputString[1] }
{ 1140005;Label ;220 ;2090 ;3300 ;440 ;ParentControl=1140004 }
{ 1140024;TextBox ;3630 ;2640 ;11990;440 ;CaptionML=DEU=Output string 2;
SourceExpr=outputString[2] }
{ 1140025;Label ;220 ;2640 ;3300 ;440 ;ParentControl=1140024 }
{ 1140026;TextBox ;3630 ;3190 ;11990;440 ;CaptionML=DEU=Output string 3;
SourceExpr=outputString[3] }
{ 1140027;Label ;220 ;3190 ;3300 ;440 ;ParentControl=1140026 }
{ 1140028;TextBox ;3630 ;3740 ;11990;440 ;CaptionML=DEU=Output string 4;
SourceExpr=outputString[4] }
{ 1140029;Label ;220 ;3740 ;3300 ;440 ;ParentControl=1140028 }
{ 1140030;TextBox ;3630 ;4290 ;11990;440 ;CaptionML=DEU=Output string 5;
SourceExpr=outputString[5] }
{ 1140031;Label ;220 ;4290 ;3300 ;440 ;ParentControl=1140030 }
{ 1140032;TextBox ;3630 ;4840 ;11990;440 ;CaptionML=DEU=Output string 6;
SourceExpr=outputString[6] }
{ 1140033;Label ;220 ;4840 ;3300 ;440 ;ParentControl=1140032 }
{ 1140034;TextBox ;3630 ;5390 ;11990;440 ;CaptionML=DEU=Output string 7;
SourceExpr=outputString[7] }
{ 1140035;Label ;220 ;5390 ;3300 ;440 ;ParentControl=1140034 }
{ 1140036;TextBox ;3630 ;5940 ;11990;440 ;CaptionML=DEU=Output string 8;
SourceExpr=outputString[8] }
{ 1140037;Label ;220 ;5940 ;3300 ;440 ;ParentControl=1140036 }
{ 1140038;TextBox ;3630 ;6490 ;11990;440 ;CaptionML=DEU=Output string 9;
SourceExpr=outputString[9] }
{ 1140039;Label ;220 ;6490 ;3300 ;440 ;ParentControl=1140038 }
{ 1140040;TextBox ;3630 ;7040 ;11990;440 ;CaptionML=DEU=Output string 10;
SourceExpr=outputString[10] }
{ 1140041;Label ;220 ;7040 ;3300 ;440 ;ParentControl=1140040 }
{ 1140006;CommandButton;5940;1100;2200;550 ;CaptionML=DEU=Split;
OnPush=BEGIN
Split(inputString,outputString,maxoutLen);
END;
}
}
CODE
{
VAR
inputString@1140000 : Text[250];
outputString@1140001 : ARRAY [10] OF Text[250];
maxoutLen@1140002 : Integer;
index@1140003 : Integer;
maxIndex@1140004 : Integer;
splitPosition@1140005 : Integer;

PROCEDURE [Split@1140001(p_inputStr@1140000](mailto:Split@1140001(p_inputStr@1140000) : Text[250];VAR p_outputStr@1140001 : ARRAY [10] OF Text[250];p_maxLen@1140002 : Integer);
BEGIN
IF p_maxLen <= 0 THEN
ERROR(’“Max. Output Length” has to be greater than zero.’);

CLEAR(p_outputStr);
index := 0;
maxIndex := ARRAYLEN(p_outputStr);

REPEAT
index := index + 1;
splitPosition := STRLEN(p_inputStr);
IF splitPosition > p_maxLen THEN BEGIN
splitPosition := p_maxLen;
IF (COPYSTR(p_inputStr,splitPosition,1) <> ’ ‘) AND // split within word
(COPYSTR(p_inputStr,splitPosition + 1,1) <> ’ ‘)
THEN BEGIN
REPEAT
splitPosition := splitPosition - 1;
UNTIL (COPYSTR(p_inputStr,splitPosition,1) = ’ ‘) OR (splitPosition <= 1);
IF COPYSTR(p_inputStr,splitPosition,1) <> ’ ’ THEN
ERROR(‘Input string contains a word which is too long: “%1”’,p_inputStr);
END;
END;
p_outputStr[index] := DELCHR(COPYSTR(p_inputStr,1,splitPosition),’<>’,’ ‘);
p_inputStr := DELCHR(COPYSTR(p_inputStr,splitPosition + 1),’<>’,’ ');
UNTIL (p_inputStr = ‘’) OR (index >= maxIndex);

IF p_inputStr <> ‘’ THEN // better use a temporary record instead of p_outputStr
ERROR(‘Array overflow’);
END;

BEGIN
END.
}
}

Hi, thanks a lot to both of you! It works.

Rashed, There was just a little mistake in your function.

FindLastSpace(String : Text[100]) : Integer
For i := 1 to strlen(String) Do begin
if String[strlen(String)+1-i] = ’ ’ then
exit(strlen(String)-i);
end;

@ghuebner: 3 Dimension assignments missed to compile the form, but it also works great to test!

Have a nice weekend!