Jump to content

# Split String

## Recommended Posts

Good Day,
MyCode in Database = VARCHAR(20)

MyCode could be

(120 or 250)  like 3 digit Integer,  or

(200.001  or  600.010)  there is a dot between numbers,  or

(500.005.0010 or 100.010.1500)   there are 2 dots between numbers

I need to Increase MyCode by 1  that is

if MyCode = 100 then requested result = 101

if MyCode = 300.001 then requested result = 300.002

if MyCode = 600.005.0010 then requested result = 600.005.0011

How can i do that ?
Thank You

#### Share this post

##### Link to post

I would suggest to look up TryStrToInt. But that is just the beginning. What happens if you add 1 to 999.9999? Are there rules for those grouping dots? If the dots are always at the same place you could remove them before conversion and put them back in after your math and reconversion to string.

#### Share this post

##### Link to post
Posted (edited)
34 minutes ago, Henry Olive said:

600.005.0011

1. split the string based on the delimiter
2. for the last string in the list, ('0011')
3. convert to integer (11) and increment (12)
4. convert the int back to a string ('12')
5. compare the lengths and then loop to add in the padding zeros ('0012')
6. concate the list back to 1 string
Edited by SwiftExpat
note to concat

#### Share this post

##### Link to post
2 hours ago, Henry Olive said:

Good Day,
MyCode in Database = VARCHAR(20)

MyCode could be

(120 or 250)  like 3 digit Integer,  or

(200.001  or  600.010)  there is a dot between numbers,  or

(500.005.0010 or 100.010.1500)   there are 2 dots between numbers

I need to Increase MyCode by 1  that is

if MyCode = 100 then requested result = 101

if MyCode = 300.001 then requested result = 300.002

if MyCode = 600.005.0010 then requested result = 600.005.0011

How can i do that

Do it on the string itself, like you learned addition in school.

```function incrementID(const aID: string):string;
var
ch: Char;
N: Integer;
begin
Result := aID;
N:= Length(Result);
while N > 0 do begin
ch := Result[N];
case ch of
'0'..'8': begin
Result[N] := Succ(ch);
Break;
end;
'9': begin
Result[N] := '0';
Dec(N);
end;
'.': Dec(N);
else
raise Exception.CreateFmt('Invalid character "%s" in ID "%s" at position %d.',
[ch, aID, N]);
end; {case ch}
end;  {while}
end;```

• 2

#### Share this post

##### Link to post

Thank You  Sherlock, SwiftExpat

Thank You SO MUCH  Peter

#### Share this post

##### Link to post
Posted (edited)

[wrong topic]

Edited by mvanrijnen

#### Share this post

##### Link to post
Posted (edited)

Hi, yes you can Split

```procedure TForm23.Button1Click(Sender: TObject);
var S : TArray<String>;
begin
S:=Edit3.Text.Split(['.']);
S[length(S)-1]:=Format('%.3d',[(StrToInt(S[Length(S)-1])+1)]);
Edit3.Text:=String.Join('.',S);
end;```

Edited by Serge_G

#### Share this post

##### Link to post
Quote
```
Edit3.Text:=String.Join('.',[S[0],S[1],S[3]]);```

Edit3.Text:=String.Join('.',S);

?

• 1

#### Share this post

##### Link to post
Posted (edited)

@Fr0sT.Brutal, code was edited during your post.

Just a thing, this    MyCode = 600.005.0010 then requested result = 600.005.0011  should not work you have to manage a way to have the good formatstring

something like

```procedure TForm23.Button1Click(Sender: TObject);
var S : TArray<String>;
fmt : string;
begin
S:=Edit3.Text.Split(['.']);
fmt:='%.'+Length(S[Length(S)-1]).tostring+'d';
S[length(S)-1]:=Format(fmt,[(StrToInt(S[Length(S)-1])+1)]);
Edit3.Text:=String.Join('.',S);
end;```

Edited by Serge_G
• 1

#### Share this post

##### Link to post

Thank you SO MUCH  Serge

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×