question for kretzschmar...

Hello,

   I see you have alot of points so I thought I would ask you a question. I throw a value into a string something like 10.06 . I know it is an integer but I want to hold the value in a string. With this value in the string I want to take the last two character 06 and move them into another variable. This is probably very easy to do but I am not finding the answer. What do you think?

MrError
MrErrorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kretzschmarConnect With a Mentor Commented:
just pasted from your other q

hi MrError,

a function which so both

//Function for split strings
function SplitIt(Splitter : String;
                 Var ToSplit : String) : String;
Var P : Integer;
begin
  P := Pos(Splitter, ToSplit);
  If P = 0 then P := Length(ToSplit)+1;
  Result := Copy(ToSplit,1,p-1);
  ToSplit := Copy(ToSplit,p+length(splitter),MaxLongInt);
end;

example

mayAfter := MyHoleString;
myBefore := SplitIt('.',MyAfter);

MyAfter holds now only this after the point

meikl
0
 
MrErrorAuthor Commented:
Edited text of question.
0
 
denizcanCommented:
Two ways:
 - if you use integer instead of string
  var2 := var1 mod 100;
 - if you use string still
  var2 := Copy(var1, 4, 2);

By the way 10.06 is not integer...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kretzschmarCommented:
hi,

i would do something like this

  if pos('.',Edit1.Text) > 0 then
    edit2.text := copy(Edit1.text,pos('.',Edit1.Text)+1,MaxLongInt);

this would take all after the point to the second var(edit2). if you want only 2 didits, then replace maxlongint with 2

meikl
0
 
denizcanCommented:
good answer kretzschmar, I was going to bed before writing my  code..

But one thing...
for optimization

Index := Pos('.', Edit1.Text);
if Index > 0 then
  Edit2.Text := Copy(Edit1.Text, Index + 1, MaxLongInt);

this will generate less code... Because using Pos func two times causes two calls to that func...
0
 
kretzschmarCommented:
yup denizcan,

thats true, this optimization will solve some nanoseconds ;-)
(well it depends how often its called,
therefore you're correct)

meikl ;-)
0
 
MrErrorAuthor Commented:
denizcan & kretzschmar,

   Thank you for both your ideas I will try using them soon. Uhhh oh yeah 10.06 is a float not an integer.
   denizcan what type is the index?  is it a string, integer?
Index := Pos('.', Edit1.Text);

MrError
0
 
kretzschmarCommented:
hi  mrerror,

var
  index : Integer;

the function pos geives you the position of the given substring
('.' in this case) in a String (second parameter) as integer. if the
substring is not in the string the pos-function returns 0.

to solve processortime denizcan used the variable index to
store the position by the calling the pos-function once, and then
compare and action by usage of this var. cost less time than call
the pos-function twice.

well the difference is minimal,
but by exzessive usage, the better solution

meikl ;-)
0
 
MrErrorAuthor Commented:
kretzschmar,

 Ok this looks good. One more question how can I grab the first two digits? Since I am splitting this string up I will need both the values before the '.' and the ones after the '.' .

  Index := Pos('.', Edit1.Text);
  if Index > 0 then
      x := strtoint(Copy(Edit1.Text, Index + 1, MaxLongInt));

  Instead if a plus do I use - 1?

MrError
0
 
MrErrorAuthor Commented:
meikl,
 
   Man you are good. Thanks for your quick reply/solution to my problem. I think you could make a living at this programming thing.

MrError
0
All Courses

From novice to tech pro — start learning today.