Recent

Author Topic: [Solved] trim string - delete first(s) spaces  (Read 6351 times)

coimbras

  • New Member
  • *
  • Posts: 14
[Solved] trim string - delete first(s) spaces
« on: May 24, 2018, 11:14:50 pm »
Hello everyone.

I started programming Lazarus today, i´m from Portugal, escuse some bad english.

I see a problem in my code.

I try to read a simple txt file, the first char is a space in all lines (for viewing purposes i substituted the space for a underscore). The file is something like:
_1_2_3
_4_5__6_
_7_8_9

The code separates the numbers by spaces, also delete the first space on each row, EXCEPT in first row, and also don´t care if there are two following spaces like in 2nd row (thats exactly what i want), also cuts the last space in row (good!).

The problem is the first(s) spaces if they appear in first rows, the program ignores it.
I´ve changed the code by posicioning the TRIM function in many ways, but same result...

Any ideias??
Thanks.

Code: Pascal  [Select][+][-]
  1. try
  2.  
  3.       while not eof(ficheiro3) do
  4.        begin
  5.  
  6.           readln(ficheiro3, c);
  7.  
  8.         //ShowMessage(c[1]);
  9.  
  10.         Lista1.Delimiter := ' ';
  11.         Lista1.DelimitedText := Trim(c);
  12.         //Teste
  13.         ShowMessage(Lista1[0]);

« Last Edit: June 08, 2018, 06:18:54 pm by coimbras »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: trim string - delete first(s) spaces
« Reply #1 on: May 24, 2018, 11:45:06 pm »
There are also functions trimleft() and trimright().
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: trim string - delete first(s) spaces
« Reply #2 on: May 24, 2018, 11:57:01 pm »
I don't understand what you want to achieve. Do you want to separate all chars? Or do you want to keep the lines together and just remove leading and trailing spaces?

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: trim string - delete first(s) spaces
« Reply #3 on: May 25, 2018, 12:22:04 am »
First off, if you replace the spaces in the file with "_" there is no way for "Trim" to remove the leading and trailing spaces, because you removed them already.

 Please show us an example line of text that shows BEFORE condition and how you want it to look afterwards.

The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: trim string - delete first(s) spaces
« Reply #4 on: May 25, 2018, 12:31:44 am »
@ First off, if you replace the spaces in the file with "_" there is no way for "Trim" to remove the leading and trailing spaces, because you removed them already.

He replaced them only in his post, I guess.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

coimbras

  • New Member
  • *
  • Posts: 14
Re: trim string - delete first(s) spaces
« Reply #5 on: May 25, 2018, 12:52:16 am »
I don't understand what you want to achieve. Do you want to separate all chars? Or do you want to keep the lines together and just remove leading and trailing spaces?

Hello.

I wanto to put each number in a array. It´s done, but i have "first cell" filled with an space (string).


Thanks,

coimbras

  • New Member
  • *
  • Posts: 14
Re: trim string - delete first(s) spaces
« Reply #6 on: May 25, 2018, 12:55:18 am »
First off, if you replace the spaces in the file with "_" there is no way for "Trim" to remove the leading and trailing spaces, because you removed them already.

 Please show us an example line of text that shows BEFORE condition and how you want it to look afterwards.

Hello i putted a underscore in the text file just for viewing purposes, in real txt where is an _ is a space.

Here is all procedure:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3.       AssignFile(ficheiro3, nomeficheiro3);
  4.       contador:=0;
  5.       reset(ficheiro3);
  6.       Lista1 := TStringList.Create;
  7.  
  8.    try
  9.  
  10.       while not eof(ficheiro3) do
  11.        begin
  12.  
  13.           readln(ficheiro3, c);
  14.  
  15.         //ShowMessage(c[1]);
  16.  
  17.         Lista1.Delimiter := ' ';
  18.         Lista1.DelimitedText := Trim(c);
  19.         //Teste
  20.         //ShowMessage(Lista1[0]);
  21.         //ShowMessage(Lista1[1]);
  22.         //ShowMessage(Lista1[2]);
  23.         //Teste
  24.         x:=Lista1.Count;
  25.           for colunas:=0 to x-1 do
  26.             begin
  27.              //ShowMessage(Lista1[colunas]);
  28.              Tabela[contador,colunas]:=Lista1[colunas];
  29.             end;
  30.         //Lista1.Free;
  31.         contador:=contador+1;
  32.        end;
  33.       CloseFile(ficheiro3);
  34.     except
  35.       on E: EInOutError do
  36.       writeln('File handling error occurred. Details: ', E.Message);
  37.     end;
  38.     Label3.Caption:= Tabela[0,0];
  39.     Label4.Caption:= Tabela[0,1];
  40.     Label5.Caption:= Tabela[0,2];
  41.     Label6.Caption:= Tabela[1,0];
  42.     Label7.Caption:= Tabela[1,1];
  43.     Label8.Caption:= Tabela[1,2];
  44.     Label9.Caption:= Tabela[2,0];
  45.     Label10.Caption:= Tabela[2,1];
  46.     Label11.Caption:= Tabela[2,2];
  47.  
  48. end;


Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: trim string - delete first(s) spaces
« Reply #7 on: May 25, 2018, 12:57:11 am »
Hi

Maybe something like

Code: Pascal  [Select][+][-]
  1. const my_delim=' ';
  2. var s:string;
  3. s:='   1 2 3    4  5  6  7 8 9';
  4. //remove the beginning spaces
  5. s:=TrimLeft(s);
  6. showmessage('|'+s+'|');
  7. // replace any double spaces with single spaces
  8. while pos(my_delim+my_delim,s)>0 do s:=stringreplace(s, my_delim+my_delim,my_delim,[rfReplaceAll, rfIgnoreCase]);
  9. showmessage('|'+s+'|');          
  10.  


edit:
or as a function
Code: Pascal  [Select][+][-]
  1. function tidymystring(s:string;my_delim:char):string;
  2. begin
  3.   s:=trimleft(s);
  4.   if my_delim<>chr(0) then   // if delimeter is #0 then only do a trimleft
  5.   begin
  6.     while pos(my_delim+my_delim,s)>0 do s:=stringreplace(s, my_delim+my_delim, my_delim,[rfReplaceAll, rfIgnoreCase]);
  7.   end;
  8.   result:=s;
  9. end;
  10.  

use like
showmessage(tidymystring('   1 2 3   4  5   6',' '));  // second param is a space
result
1 2 3 4 5 6

showmessage(tidymystring('   1 2 3   4  5   6',chr(0))); // second param chr(0) which allows just a trimleft
result
1 2 3   4 5   6

or in your code
Code: [Select]
readln(ficheiro3, c);
c:=tidymystring(c,' '); // second param is a space
Lista1.Delimiter := ' ';
Lista1.DelimitedText := c;
x:=Lista1.Count;
« Last Edit: May 25, 2018, 01:23:35 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: trim string - delete first(s) spaces
« Reply #8 on: May 25, 2018, 02:04:14 am »
It looks like he is looking for FORMATED output...

Var

 S:string;
Begin
 Str(StrToInt(The_Number_Str):10:3, S);

 The resulting number will be formatted with a total of 10 characters, 3 of which will be decimals if you wish..
Or
 Str(StrToInt(The_Number_Str):10, S);

S will equal the results.
The only true wisdom is knowing you know nothing

reinerr

  • New Member
  • *
  • Posts: 37
Re: trim string - delete first(s) spaces
« Reply #9 on: May 25, 2018, 04:56:19 am »
coimbras, your latest code seems to work - do you still have a problem (its unclear in your last message). I ran your code by just plugging in your 3 lines manually and they all seemed fine - multiple spaces (delimiter) were treated as one.

However, you need a try..finally for the Lista1 := TStringList.Create; and one after your file Reset in case of an exception before the CloseFile.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: trim string - delete first(s) spaces
« Reply #10 on: May 25, 2018, 08:36:16 am »
Wow!
9 Replies to a simple Split-String-Into-Array-Issue.
Everyone laughed at me, when i said i wrote my own Split-String-Function
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

coimbras

  • New Member
  • *
  • Posts: 14
Re: trim string - delete first(s) spaces
« Reply #11 on: May 25, 2018, 11:27:50 am »
Hello again.

Well i substitute the c string read from text for a "inserted" on code and it works (example c:=' 1  2   3    '), with or without the function "tidymystring"... strange... so...
I start to check some thinks, i concluded that the problem is in the txt... if it is recorded in ANSI it works fine (cut the spaces at begenning and repeated, and split the numbers)), if it is recorded as UTF-8 it does not work as expected to trim the initial space.
And why i was recording the txt file in UTF-8? Well because i was testing in other file reading text with accents (Portuguese language- "Cão último país"). If recorded in ANSI dont read the accents.

Thanks for all. As i said at begenning, i´m just trying to program in Lazarus, as alternative to VB net, just need some simple programs with light interface, mainly to read data and to make something wit that data and then record the results...at maximum draw some simple graphics... the VB Net comunity put in disk at least 3 GB, it absurd.
I program manily in Lua now, its very very fast, but dont have a GUI by default and dont make exe. For me is fine, but to send to other in my company, its bad.

Thanks again.

Hi

Maybe something like

Code: Pascal  [Select][+][-]
  1. const my_delim=' ';
  2. var s:string;
  3. s:='   1 2 3    4  5  6  7 8 9';
  4. //remove the beginning spaces
  5. s:=TrimLeft(s);
  6. showmessage('|'+s+'|');
  7. // replace any double spaces with single spaces
  8. while pos(my_delim+my_delim,s)>0 do s:=stringreplace(s, my_delim+my_delim,my_delim,[rfReplaceAll, rfIgnoreCase]);
  9. showmessage('|'+s+'|');          
  10.  


edit:
or as a function
Code: Pascal  [Select][+][-]
  1. function tidymystring(s:string;my_delim:char):string;
  2. begin
  3.   s:=trimleft(s);
  4.   if my_delim<>chr(0) then   // if delimeter is #0 then only do a trimleft
  5.   begin
  6.     while pos(my_delim+my_delim,s)>0 do s:=stringreplace(s, my_delim+my_delim, my_delim,[rfReplaceAll, rfIgnoreCase]);
  7.   end;
  8.   result:=s;
  9. end;
  10.  

use like
showmessage(tidymystring('   1 2 3   4  5   6',' '));  // second param is a space
result
1 2 3 4 5 6

showmessage(tidymystring('   1 2 3   4  5   6',chr(0))); // second param chr(0) which allows just a trimleft
result
1 2 3   4 5   6

or in your code
Code: [Select]
readln(ficheiro3, c);
c:=tidymystring(c,' '); // second param is a space
Lista1.Delimiter := ' ';
Lista1.DelimitedText := c;
x:=Lista1.Count;

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: trim string - delete first(s) spaces
« Reply #12 on: May 25, 2018, 12:11:18 pm »
Overly complex and if the Ansi codepage is properly set you can also do this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;
  3. var
  4.  s:string = '   1 2 3   4  5   6 ';
  5.  sa:TStringArray;
  6. begin
  7.  s:= s.Replace('  ',' ',[rfReplaceAll]);
  8.  sa :=s.Split([#32]);
  9.  for s in sa do
  10.    if s <> '' then writeln(s);
  11. end.

The helpers in sysutils already have a myriad of split functions...
« Last Edit: May 25, 2018, 01:49:28 pm by Thaddy »
Specialize a type, not a var.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: trim string - delete first(s) spaces
« Reply #13 on: May 25, 2018, 12:13:24 pm »
Hi

You could use the lazutf8 unit; and then use the utf8 functions.

Code: Pascal  [Select][+][-]
  1. uses .........,lazutf8;
  2.  
  3. function tidymystring(s:string):string;
  4. begin
  5.   s:=utf8trim(s);
  6.   while utf8pos('  ',s)>0 do s:=utf8stringreplace(s, '  ',' ',[rfReplaceAll, rfIgnoreCase]);
  7.   result:=s;
  8. end;
  9.  
  10.  
  11. begin
  12.   showmessage(tidymystring('    Cão      último     país   '));
  13.   showmessage(tidymystring('   1 2 3     4  5    6'));
  14. end;                                                      
  15.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: trim string - delete first(s) spaces
« Reply #14 on: May 25, 2018, 12:26:39 pm »
Why would you use UTF8 functions when the inputstring is ASCII???

Bart

 

TinyPortal © 2005-2018