How to delete last char

后端 未结 5 1191
醉酒成梦
醉酒成梦 2021-01-12 15:24

How to delete last character of String with substring or anything ?

For example;

var
  query : String;
begin
  query:= \'test=1&line         


        
相关标签:
5条回答
  • 2021-01-12 15:53

    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)

    0 讨论(0)
  • 2021-01-12 15:56

    A third option is this:

    SetLength(Query,LENGTH(Query)-1)
    
    0 讨论(0)
  • 2021-01-12 16:03

    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.

    0 讨论(0)
  • 2021-01-12 16:04

    Try this:

    var
      query : String;
    begin
      query:= 'test=1&line=5&';
      delete(query,length(query),1);
    
    0 讨论(0)
  • 2021-01-12 16:04
    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;

    0 讨论(0)
提交回复
热议问题