Copy part of a string

Asked

Viewed 16,806 times

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 do a function to copy everything after : (two points) forward. Is it 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.

4 answers

3


You can use the function Copy combined with the function Pos

TextoOriginal := 'Cliente em questão 3: 20392039230902032032';
valorTexto := Copy(TextoOriginal , Pos(':', TextoOriginal) + 1, Length(TextoOriginal));
  • 1

    good at the time I tried to do this did not know the function pos lol

  • @Caputo, gave error in the variable valueText, is saying that parameters are missing.

  • had a wrong parentheses @user7605. I already fixed

  • 1

    Perfect @Caputo, thank you, and thank you to everyone who helped!

0

I don’t know how it works in Delphi but in PHP I would use the "explode" function that you define 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] Client in question [1] protocolo20209092032932

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

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

  • thanks for the reply, I am looking for if I find something on the internet similar to what you said!

  • is it possible to put that there inside a while ? because it is several lines..

  • If it has more than one ":" will not make a difference, the only thing that will change is the position of the result in the array, in the example I gave the result you want would be in the $result[2]. In PHP it is possible to put in a while yes already in Delphi I do not know how it works but it is very likely that it is also possible.

  • in PHP how would I look inside a while ? I’ll do in PHP itself...

0

look q easy in php, in my example the strings would be an input.txt file

input file.txt

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

file . php

<?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
)
  • and if the string is twice the ":" ? How to do ? now that I’ve seen it has twice the :.... I need to take the second ":"..... type: Customer: in question 3: 2039203923090203

  • I edited the code to adapt to more than one ":", can be three or more.

0

I did that a thousand years ago!

At the time I created a text search function, to ensure that 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;

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

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

Do tests, it’s been so long that I don’t use Delphi I’m rusty!

  • where I will set this function that I need to look for after the ":" two points ?

  • look here if BuscaTexto(MsgOriginal,':') = 'ok' then if it finds two points it will return to position and you use the copy to cut where you want

Browser other questions tagged

You are not signed in. Login or sign up in order to post.