Recent

Author Topic: select string from begin to third space character  (Read 6231 times)

addr3ss

  • New Member
  • *
  • Posts: 18
select string from begin to third space character
« on: July 03, 2015, 12:35:17 pm »
Hello!
How can I select or copy the beginner part of a string from character 1 to third space character?
for example:
in "IN THE NAME OF GOD"
select from "I"(character 1) to "O"(third space character).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: select string from begin to third space character
« Reply #1 on: July 03, 2015, 01:02:45 pm »
You could learn how to use regular expressions.
Or you could roll your own little parsing function. Numerous ways to do that, such as

Code: [Select]
function CopyTo3rdSpace(aText: string): string;
const
  Space = ' ';
var
  len: integer;
  spaceCount: integer = 0;
  i: integer = 1;
begin
  len:=Length(aText);
  if (len < 7) then
    Exit('');
  pc:=PChar(aText);
  repeat
    if (aText[i] = Space) then
      Inc(spaceCount);
    Inc(i);
    if (spaceCount = 3) and (i <= len) then
      begin
        Result:=Copy(aText, 1, i);
        Exit;
      end;
until (i > len);
  Result:='';
end;

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: select string from begin to third space character
« Reply #2 on: July 03, 2015, 01:21:50 pm »
or use leftstr midstr and the rest of the rtl strutils.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

addr3ss

  • New Member
  • *
  • Posts: 18
Re: select string from begin to third space character
« Reply #3 on: July 03, 2015, 02:12:17 pm »
You could learn how to use regular expressions.
Or you could roll your own little parsing function. Numerous ways to do that, such as

Code: [Select]
function CopyTo3rdSpace(aText: string): string;
const
  Space = ' ';
var
  len: integer;
  spaceCount: integer = 0;
  i: integer = 1;
begin
  len:=Length(aText);
  if (len < 7) then
    Exit('');
  pc:=PChar(aText);
  repeat
    if (aText[i] = Space) then
      Inc(spaceCount);
    Inc(i);
    if (spaceCount = 3) and (i <= len) then
      begin
        Result:=Copy(aText, 1, i);
        Exit;
      end;
until (i > len);
  Result:='';
end;
OK tnx worked.but I want to copy strings that has under 3 spaces completely and copy the beginner part of strings that has 3 spaces and above.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: select string from begin to third space character
« Reply #4 on: July 03, 2015, 05:27:21 pm »
OK tnx worked.but I want to copy strings that has under 3 spaces completely and copy the beginner part of strings that has 3 spaces and above.
My Code would be:
Code: [Select]
function CopyTonthSpace(aText: string;MaxSpace:integer=3): string;
const
  Space = ' ';
var
  i: integer;
  PartStr: String;
  spacepos: SizeInt;
//  pc: PChar;
begin
  Result := '';
  // If no spaces to look for, copy string;
  if MaxSpace = 0 then
    begin
      spacepos :=0;
      result := aText;
    end
  else
  // Build result Word by Word
  for i := 1 to MaxSpace do
    begin
      PartStr :=RightStr(aText,length(atext)-length(result));
      spacepos := pos(space,PartStr);
      if spacepos <> 0 then
        result := result + copy(PartStr,1,spacepos)
      else
        begin
          result := atext;
          break;
        end;
    end;
  // Copy the char after the last space
  if Spacepos <> 0 then
    result := result + copy(PartStr,spacepos+1,1);
end;
Results:
  CopyTonthSpace('In the Name of God') := 'In the Name o'
  CopyTonthSpace('In my Name') := 'In my Name'
  CopyTonthSpace('In my Name',0) := 'In my Name'

OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

addr3ss

  • New Member
  • *
  • Posts: 18
Re: select string from begin to third space character
« Reply #5 on: July 04, 2015, 09:59:52 am »
OK.I changed the code to this and worked.
Code: [Select]
repeat
    if string[i]=space then
       Inc(spacecount);
    Inc(i);
    if (spacecount<3)and(i<=Length(string)) then begin
    Label2.Caption:=string;
    end
    else if (spacecount=3)and (i<=Length(string)) then
       begin
       Label2.Caption:=Copy(string,1,i);
       Exit;
       end;
  until (i>Length(string));

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: select string from begin to third space character
« Reply #6 on: July 04, 2015, 11:25:39 am »
OK.I changed the code to this and worked.
That's OK, But your code has 2 problems.
first you should not name you variable after a pascal-basetype like string and integer. 
I put your code in a test-Function
Code: [Select]
function TestCode(aText: string;MaxSpace:integer=3): string;
// I assumed these variables are initialized here
const
  Space = ' ';
var i:integer=0;
    spacecount:integer=0;
begin
repeat
    if aText[i]=space then
       Inc(spacecount);
    Inc(i);
    if (spacecount<MaxSpace)and(i<=Length(aText)) then begin
    result:=aText;
    end
    else if (spacecount=MaxSpace)and (i<=Length(aText)) then
       begin
       result:=Copy(aText,1,i);
       Exit;
       end;
  until (i>Length(aText));
end;
And test it against:
Code: [Select]
  writeln(TestCode('In the Name of God'));
  writeln(TestCode('In the Name of God, maker of all'));
  writeln(TestCode('In my Name'));
  writeln(TestCode('In my Name ')+'<');
  writeln(TestCode('In my Name  ')+'<');
  writeln(TestCode('In my Name',0)); // you'd expect the whole test but you only get 'I'
  writeln(TestCode('In  my Name'));
  writeln(TestCode('')+'<'); // here you get a SIGSEGV
I assumed your string-variable is a string, then you get a Segmentation Fault (SIGSEGV)
if you use shortstring instead you don't get that fault but the line 2 output 'In the N' instead of 'In the Name o'

Better do a length-check before "aText[ i ]"
Code: [Select]
if length(aText) = 0 then
  begin
     result := aText;
     exit; // see next comment about exit.
  end;
or
Code: [Select]
if (length(aText) >0) and (aText[i]=space) then
You should use exit with care.
In your case, if you only want to go to the end of the repeat-until construct, use break instead of exit
so if you want to execute code after until, it's not affected by the string.

Have you thought of other Whitespaces like <tab> ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

addr3ss

  • New Member
  • *
  • Posts: 18
Re: select string from begin to third space character
« Reply #7 on: July 04, 2015, 11:58:08 am »
OK.I changed the code to this and worked.
That's OK, But your code has 2 problems.
first you should not name you variable after a pascal-basetype like string and integer. 
I put your code in a test-Function
Code: [Select]
function TestCode(aText: string;MaxSpace:integer=3): string;
// I assumed these variables are initialized here
const
  Space = ' ';
var i:integer=0;
    spacecount:integer=0;
begin
repeat
    if aText[i]=space then
       Inc(spacecount);
    Inc(i);
    if (spacecount<MaxSpace)and(i<=Length(aText)) then begin
    result:=aText;
    end
    else if (spacecount=MaxSpace)and (i<=Length(aText)) then
       begin
       result:=Copy(aText,1,i);
       Exit;
       end;
  until (i>Length(aText));
end;
And test it against:
Code: [Select]
  writeln(TestCode('In the Name of God'));
  writeln(TestCode('In the Name of God, maker of all'));
  writeln(TestCode('In my Name'));
  writeln(TestCode('In my Name ')+'<');
  writeln(TestCode('In my Name  ')+'<');
  writeln(TestCode('In my Name',0)); // you'd expect the whole test but you only get 'I'
  writeln(TestCode('In  my Name'));
  writeln(TestCode('')+'<'); // here you get a SIGSEGV
I assumed your string-variable is a string, then you get a Segmentation Fault (SIGSEGV)
if you use shortstring instead you don't get that fault but the line 2 output 'In the N' instead of 'In the Name o'

Better do a length-check before "aText[ i ]"
Code: [Select]
if length(aText) = 0 then
  begin
     result := aText;
     exit; // see next comment about exit.
  end;
or
Code: [Select]
if (length(aText) >0) and (aText[i]=space) then
You should use exit with care.
In your case, if you only want to go to the end of the repeat-until construct, use break instead of exit
so if you want to execute code after until, it's not affected by the string.

Have you thought of other Whitespaces like <tab> ?
no.it is just space.would you give me the correct code with this result?i tested it and its worked but i dont know what i should do for better performance.

addr3ss

  • New Member
  • *
  • Posts: 18
Re: select string from begin to third space character
« Reply #8 on: July 04, 2015, 12:57:36 pm »
oh...yes,i readed your advice carefully and found out.tnx

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: select string from begin to third space character
« Reply #9 on: July 04, 2015, 12:58:52 pm »
no.it is just space.would you give me the correct code with this result?i tested it and its worked but i dont know what i should do for better performance.
What is wrong with the function I gave you ?
Code: [Select]
function CopyTonthSpace(aText: string;MaxSpace:integer=3): string;
const
  Space = ' ';
var
  i: integer;
  PartStr: String;
  spacepos: SizeInt;
//  pc: PChar;
begin
  Result := '';
  // If no spaces to look for, copy string;
  if MaxSpace = 0 then
    begin
      spacepos :=0;
      result := aText;
    end
  else
  // Build result Word by Word
  for i := 1 to MaxSpace do
    begin
      PartStr :=RightStr(aText,length(atext)-length(result));
      spacepos := pos(space,PartStr);
      if spacepos <> 0 then
        result := result + copy(PartStr,1,spacepos)
      else
        begin
          result := atext;
          break;
        end;
    end;
  // Copy the char after the last space
  if Spacepos <> 0 then
    result := result + copy(PartStr,spacepos+1,1);
end;
TestCases:
Code: [Select]
  writeln(CopyTonthSpace('In the Name of God'));
  writeln(CopyTonthSpace('In the Name of God, maker of all'));
  writeln(CopyTonthSpace('In my Name'));
  writeln(CopyTonthSpace('In my Name ')+'<');
  writeln(CopyTonthSpace('In my Name  ')+'<');
  writeln(CopyTonthSpace('In my Name',0));
  writeln(CopyTonthSpace('In  my Name'));
  writeln(CopyTonthSpace('')+'<');
are good, performance is still ok since you/I copy word-blocks and not single character.
Generally put function first, performance later. 
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: select string from begin to third space character
« Reply #10 on: July 04, 2015, 01:00:04 pm »
Is this somehow related to your .resx- problem ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

addr3ss

  • New Member
  • *
  • Posts: 18
Re: select string from begin to third space character
« Reply #11 on: July 04, 2015, 01:21:24 pm »
No.they are differents.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: select string from begin to third space character
« Reply #12 on: July 04, 2015, 01:49:02 pm »
oh...yes,i readed your advice carefully and found out.tnx
Read also about
http://wiki.freepascal.org/fpcunit

So you could always test your units/functions/object/classes
To see that they keep working as expected.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

 

TinyPortal © 2005-2018