Solved

about SubStr

Posted on 2004-04-08
12
973 Views
Last Modified: 2010-04-05
While reading, I came across a line of codes which I don't understand.
Can somebody explain to me what the below codes are doing?

Trim(SubStr(myText.Strings[i], 2, ';'));
0
Comment
Question by:cybermilky
12 Comments
 
LVL 9

Expert Comment

by:auke_t
ID: 10789863
I think it will take the i-th line from the myText and copy the line starting at the second symbol until the first ';'.

So if mytext would be like

ASome;
BText in here; to be copied;
CWill it work;

Then Trim(SubStr(myText.Strings[1], 2, ';'));

would return 'Text in here'

But just guessing here...

You could search the program for the declaration of the SubStr function as I think it's not an official Delphi function.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10789876
Trim(..) removes leading and trailing spaces and control characters from the given string S.

SubStr must be some function returnig string.

It should be declared and defined in the code.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10789888
Trim:
Trims leading and trailing spaces and control characters from a string.

SubStr is probably a function that extracts a substring from a string using parameters (type of Integer and Char or string).
You need to find this function to see the way it works....

emil
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:rbohac
ID: 10790738
SubStr is most likely equvilent to the copy procedure in Delphi
0
 
LVL 17

Expert Comment

by:mokule
ID: 10790965
Hey guys,
don't be fortune-tellers.
It is not known from the code above what SubStr is doing.
0
 
LVL 6

Expert Comment

by:rbohac
ID: 10791083
I wasn't trying for that... just based on the code though the substr function appears as though it is defined as

function SubStr(S:String;StartPos:Integer;EndChar:Char):String;
begin
  Result := Copy(S,StartPos,Pos(EndChar,S) -StartPos);
end;

Based both on the context and how the substr function is defined in other programming languages
0
 
LVL 17

Expert Comment

by:mokule
ID: 10791117
So..
what would You tell about
It takes second section from given string delimited by ';'.
And so on and on..
0
 
LVL 6

Expert Comment

by:rbohac
ID: 10791227
What?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10791358
Imagine
str := 'aaaaaaaaaa;bbbbbbbbbbbbbbb;ccccccccccccccc;dddddddddddd'
SubStr(str,2,';')
returns
bbbbbbbbbbbbbbb
How do Uou like it?
0
 
LVL 6

Expert Comment

by:rbohac
ID: 10791507
umm.. Thats what startpos is for. Similar to both the copy command and pos. You have to do the programming yourself.

the function obviously uses a startpos of "2". The point of the function is to copy a SubString out of a larger string, not chop up the string.

In your case SubStr(str,12,';') would return bbbbbbbbbbbbbbb

Judging by the vague description posted, he is probably reading values from either a file, or from a network somehow, line by line. I say this because of the Trim statement.

If you wanted to you could extend the SubStr function and pass S as a var paramater like so

function SubStr(var S:String;StartPos:Integer;EndChar:Char;Const RemoveString:Boolean = False):String;
begin
  Result := Copy(S,StartPos,Pos(EndChar,S) -StartPos);
  if RemoveString then
    Result := Copy(Result,1,StartPos-1)+Copy(Result,StartPos + Length(Result),Length(S));
end;

Which would remove the section you return, however you would have to pass the function a string variable instead of just passing it a string.
0
 
LVL 6

Expert Comment

by:rbohac
ID: 10791555
My mistake

function SubStr(var S:String;StartPos:Integer;EndChar:Char;Const RemoveString:Boolean = False):String;
begin
 Result := Copy(S,StartPos,Pos(EndChar,S) -StartPos);
 if RemoveString then
   S := Copy(S,1,StartPos-1)+Copy(S,StartPos + Length(Result),Length(S));
end;
0
 
LVL 20

Accepted Solution

by:
Madshi earned 20 total points
ID: 10796340
Maybe that code is using madStrings (which is freeware)?

http://help.madshi.net/StringSub.htm
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Working with hours 3 65
Delphi inherited method 6 76
tvirtualstringtree freeze when load too manny images 10 71
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question