views:

274

answers:

6

How can I get some part of string that I need?

accountid=xxxxxx type=prem servertime=1256876305 addtime=1185548735 validuntil=1265012019 username=noob directstart=1 protectfiles=0 rsantihack=1 plustrafficmode=1 mirrors= jsconfig=1 [email protected] lots=0 fpoints=6076 ppoints=149 curfiles=38 curspace=3100655714 bodkb=60000000 premkbleft=25000000 ppointrate=116

I want data after email= but up to live.com.?

+1  A: 

The following code only works if values contain no spaces:

uses
  StrUtils, Classes;

....

function GetPropertyValue (const PropertyName : String; const InputString : String) : String;
var
  StringList : TStringList;
  Str : String;
begin
Result := '';
StringList := TStringList.Create;
try
  StringList.Delimiter := ' ';
  StringList.DelimitedText := InputString;
  for Str in StringList do
    if StartsText (PropertyName + '=', Str) then
      Result := RightStr (Str, Length (Str) - Length (PropertyName) - 1);    
finally
  FreeAndNil (StringList);
end;
end;
Smasher
+2  A: 

My idea:

  1. replace spaces with CRLF (it it is space separated)
  2. load into TStringList
  3. use values property with 'email' name
Michał Niklas
+11  A: 

There are a couple of ways to do this. You could split the string on the space character then feed it into TStringList. You can then use TStringList's Value[String] property to get the value of a given name.

To do that, do a string replace of all spaces with commas:

newString := StringReplace(oldString, ' ', ',', [rfReplaceAll]);

Then import the result into a TStringList:

var
  MyStringList : TStringList;
begin
  MyStringList := TStringList.Create;
  try
    MyStringList.CommaText := StringReplace(oldString, ' ', ',', [rfReplaceAll]);
    Result := MyStringList.Values['email'];
  finally
    MyStringList.Free;
  end;
end;

This will give you the email value. You'll then need to split the string at the "@" symbol which is a relatively trivial exercise. Of course, this only works if spaces are genuinely a delimiter between fields.

Alternatively you could use a regular expression but Delphi doesn't support those natively (you'd need a regex library - see here)

*** Smasher noted (D2006+) Delimiter / Delimited text which would look something like this:

MyStringList.Delimiter := ' ';
MyStringList.DelimitedText := oldString;
Result := MyStringList.Values['email'];
Steve N
+1 Nice solution. Didn't know about the `Values` property. You could just use `StringList.Delimiter` and `StringList.DelimitedText` instead of replacing spaces with commas.
Smasher
The CommaText property does not require records to be separated by commas. Whitespace characters will do just as well, so you don't need to convert spaces to commas. Better to set DelimitedText with StrictDelimiter set to true, though. Then you're allowed to have commas in the keys or values.
Rob Kennedy
A: 

Split the string into an array of string, using the '=' as the deliminator, you will then have an array with in this order: 'Key' then 'Value' you could then loop through looking for the 'email' key then simply add 1 to the array index to get the value. But this could fail in lots of ways (eg some one enters '=' as a character) or there are empty strings in the value field

Darknight
+1  A: 

Another idea, you could also use PosEx (StrUtils) with the StringList text:

function ExtractMyString(SrcStr, FromStr, ToStr: string): string;
var
  posBeg, posEnd: integer;
begin
  Result := '';
  posBeg := Pos(FromStr, SrcStr) + Length(FromStr);
  posEnd := PosEx(ToStr, SrcStr, posBeg);

  if (posBeg > 0) and (posEnd > posBeg) then
    Result := Copy(SrcStr, posBeg, posEnd-posBeg);
end;

Usage:

ExtractMyString(StringList.Text, 'email=', ' lots=');

Of course this will only work if the source string is always formatted the same way, would be handy for extracting the other data as needed.

EagleOfToledo
+1  A: 

assuming that the string is held in variable 's', and 'tmp' is another string variable,

i:= pos ('email=', s);
tmp:= '';
inc (i);
while s[i] <> ' ' do
 begin
  tmp:= tmp + s[i]; 
  inc (i);
 end;

'tmp' will hold the address

No'am Newman