Recent

Author Topic: format-reading strings  (Read 19697 times)

cazzajay

  • Jr. Member
  • **
  • Posts: 94
format-reading strings
« on: August 11, 2009, 02:01:47 pm »
i have a string which is "XXXABCYYY"

where XXX  and YYY are numbers (not the same) which may be any combination of 1, 2, or 3 digits i.e.

1
2
3
4
10
11
12
13
123
124
125
etc

the question is - how do i extract just the ABCYYY bit?

if i use rightstr(myString, 6) it works if the YYY is a 3 digit number, but obviously if its 2 or 1 digits i will get XABCYY or XXABCY respectively.

is there a way to enforce the rightstr to start at A rather than a fixed number of characters from the right hand side?

Any advice would be great, thanks!
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

gerardus

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #1 on: August 11, 2009, 06:44:56 pm »
If you are uncertain of how many digits there are, you can skip chars as long as they are digits, and then copy from that position

Code: [Select]
i := 1;
myResultString := '';
while (myString[i] in ['0'..'9']) and (i <= 3) do
  inc(i);
MyResultString := Copy(MyString, i, Length(MyString)-i);

Regards,

Gerard.
« Last Edit: August 11, 2009, 06:47:19 pm by gerardus »

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #2 on: August 11, 2009, 07:20:45 pm »
hi

first thanks for the reply, it seems like a useful tip for future!  :D

however, what if the initial bit to skip contains other characters such as spaces and symbols eg:

XXX: ABCYYY

where XXX is 1, 2, or 3 digits as above?  Would this work with that?
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

gerardus

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #3 on: August 11, 2009, 07:32:57 pm »
Quote
however, what if the initial bit to skip contains other characters such as spaces and symbols eg

No, obviously. There's no way I can help if you change the rules on the fly.  ;D
Do you have an exact grammar or spec of what goes in one of the input strings?

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #4 on: August 11, 2009, 08:07:19 pm »
hehe. touche!

ok ill explain exactly what im after doing.

i have a listbox filled with the text as follows:

Factor XX: NameYYY

and im after extracting the NameYYY bit when the user double clicks on the box using something like

str := listbox1.items[listbox1.itemindex];

except i want str to not contain the word "Factor XX:"

thanks for ur time  :D :D
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: format-reading strings
« Reply #5 on: August 11, 2009, 08:15:40 pm »
Method depends whether the String NameYYY may contain characters outside of the ASCII range like öäüè etc.

Otoh if, it's always like above, you could take the ':' as separator.
Is it this way? Then use the Pos function.
« Last Edit: August 11, 2009, 08:19:44 pm by theo »

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #6 on: August 11, 2009, 08:40:40 pm »
always in ascii.  how do i use pos to identify the colon as a separator? given XX could be 1 or 2 digits, and YYY could be 1, 2, or 3 digits?
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: format-reading strings
« Reply #7 on: August 11, 2009, 08:44:01 pm »
Pos simply finds the first occurence of a character or a string and returns its position.

http://www.freepascal.org/docs-html/rtl/system/pos.html

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #8 on: August 11, 2009, 08:46:46 pm »
legendary! thankya  8-) 8-) 8-) 8-) 8-)
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: format-reading strings
« Reply #9 on: August 11, 2009, 09:24:48 pm »
Get rid of this parsing nighmare: use regular expressions!!

Built for Delphi; works perfectly with Lazarus.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: format-reading strings
« Reply #10 on: August 11, 2009, 11:36:17 pm »
Get rid of this parsing nighmare: use regular expressions!!

It's nice but for a newbie finding a colon in a string it's overkill, don't you think?

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #11 on: August 12, 2009, 12:28:14 am »
Get rid of this parsing nighmare: use regular expressions!!

It's nice but for a newbie finding a colon in a string it's overkill, don't you think?

well its a steep learning curve, but im climbing slowly and steadily...
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #12 on: August 12, 2009, 01:03:56 am »
OK here's what ive done:

for the text in the listbox item "Factor 1: ABCXXX" where XXX could be 1, 2 or 3 digits long, ive done this code:



var
i: integer;
final: string;

begin
i := pos(': ', listbox1.Items[listbox1.itemindex]);
final := ansimidstr(listbox1.items[listbox1.itemindex], i, length(listbox1.items[listbox1.itemindex])); 
showmessage(final);
end;



using the ansimidstr that used to exist in old delphi (which doesnt exist in lazarus). 

So...anyone know whats the equivalent in lazarus? ive tried just midstr (as ansileftstr is just leftstr in lazarus, etc.) to no avail...?
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #13 on: August 12, 2009, 01:07:49 am »
solved it, i didnt have strutils in my uses. d'oh!
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: format-reading strings
« Reply #14 on: August 12, 2009, 09:23:38 am »
It's nice but for a newbie finding a colon in a string it's overkill, don't you think?

If it's only about finding a colon in a string, ever, you're right.

But this thread started with detecting a certain pattern in a substring.
And then it went on with searching for a colon in a string and extracting text based on that position.
Everytime the requirements change (e.g. 'now I want the text that is after the second colon...') you have to change the code and make it more complex. As you can see in the solution above, it gets ugly really fast!

Sure, regular expressions have a learning curve. But it's definitely worth the effort.
And I have faith in cazzajay  :D
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

 

TinyPortal © 2005-2018