Copy part of a string

0

I have the following strings:

  Cliente em questão: protocolo20209092032932
  Cliente em questão 2: protocolo320930293232
  Cliente em questão 3: 20392039230902032032

I need to make a function to copy everything that is after the: (colon) forward. it's possible ? Remember that the strings before: are not the same size, so I need to make it clear that it has to be after: (two points) forward.

    
asked by anonymous 08.08.2014 / 00:44

4 answers

3

You can use the Copy function combined with the Pos function

TextoOriginal := 'Cliente em questão 3: 20392039230902032032';
valorTexto := Copy(TextoOriginal , Pos(':', TextoOriginal) + 1, Length(TextoOriginal));
    
08.08.2014 / 14:00
0

It looks easy in php, in my example the strings would be an input.txt file

input.txt file

Cliente em questão: protocolo20209092032932
Cliente em questão 2:: protocolo320930293232
Cliente em questão 3::: 2039203923090203203
Cliente: em questão 3: 2039203923090203203

.php file

<?php

$handle = fopen("input.txt", "r");
$contents = [];

while (!feof($handle)) {
  $str_array = fgetcsv($handle, 1000, ":");      
  $str_array_count = count($str_array) - 1;      
  $last_index = $str_array[$str_array_count];

  $contents[] = $last_index;

}

fclose($handle);

print_r($contents);

?>

output

Array
(
    [0] =>  protocolo20209092032932
    [1] =>  protocolo320930293232
    [2] =>  2039203923090203203
    [3] =>  2039203923090203203
)
    
08.08.2014 / 01:09
0

I did this a thousand years ago!

At the time I created a text search function, to make sure the string I was looking for was actually inside the text.

Function BuscaTexto(Text,Busca : string) : string;
var n : integer;
begin
  for n := 1 to length(Text) do
    begin
       if Copy(Text,n,length(Busca)) = Busca then
          begin
             Result := 'ok';
             RetornoBuscaPos:=n;

          end;

    end;
end;

Note that RetornoBuscaPos will tell you which position the string was found in, then use the copy function to cut where you want it, do something like this:

  if BuscaTexto(MsgOriginal,':') = 'ok' then
    begin
    Resultado := Copy(MsgOriginal,RetornoBuscaPos,Length(MsgOriginal));
  end

Test it, it's been so long since I've used Delphi I'm rusty!

    
08.08.2014 / 14:21
0

I do not know how it works in Delphi but in PHP I would use the "explode" function that defines a delimiter and it puts everything inside an array, for example:

$cliente = "Cliente em questão: protocolo20209092032932";

$resultado = explode(':', $cliente);

This would return an array with two values:

[0] Customer in question [1] protocol20209092032932

And after that you only have to do what you have to do with your $ result variable [1]

This is just an idea of how I would do it in PHP, now just apply it to Delphi.

    
08.08.2014 / 01:00