How to delete last character of String with substring
or anything ?
For example;
var
query : String;
begin
query:= \'test=1&line
I actually was going to use this to another answer called "Delete last 5 characters from a string", probably not the best way to do this but it works for simple strings as #0 indicates a termination of a string.
for your example
Query[Length(Query)]:= #0;
For the other topic example I can'nt reply on
str[Succ(Length(str)) - 5]:= #0;
showmessage(str);
P.s I would vote HeartWare answer as that would be the more acceptable way to do this. I was just showing for simple strings a quick alternative that has the desired effects(mainly for the other topic conceringing deleting the last 5 chars)
A third option is this:
SetLength(Query,LENGTH(Query)-1)
I think I would write this as:
NewValue := OldValue.Substring(0, OldValue.Length - 1);
I think the functional interface, that is a function returning a new value, is usually to be preferred over a procedure with side-effects. I find the assignment operator is a clear indicator that a new value is being assigned to the variable. Using a functional approach makes the syntax much cleaner when you want the new value to be stored in a different variable from the original value.
Try this:
var
query : String;
begin
query:= 'test=1&line=5&';
delete(query,length(query),1);
var
test: System.String;
begin
test := 'Sample Text To Delete Last Character;!';
test := Copy(test,1,length(test)-1);
ShowMessage(test);
end;
Variable is Established;
A SampleText is provided to the Variable;
Variable then Reference itself in the Copy command which
the Copy command takes the variable string by coping it by its length from index 1(length)-1
which return the result to the variable itself;
A Message is used to show the result;
the result would be the text without exclamation; // Sample Text To Delete Last Character;