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=noob@live.com lots=0 fpoints=6076 ppoints=149 curfiles=38 curspace=3100655714 bodkb=60000000 premkbleft=25000000 ppoin开发者_运维百科trate=116
I want data after email= but up to live.com.?
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'];
My idea:
- replace spaces with CRLF (it it is space separated)
- load into TStringList
- use values property with 'email' name
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;
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.
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
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
精彩评论