如何将字符串的一部分计为特定字符?

时间:2019-04-08 10:03:59

标签: pascal

我将元素存储在数组中,格式为name#email,我想按名称搜索数组,然后仅输出电子邮件,即#.之后的内容,例如,如果当我按名称Donald搜索时,元素为donald#donald@hotmail.com,输出应为donald@hotmail.com

我的想法是从长度(名称)中减去长度(字符串)。我怎么最多只算#个?

1 个答案:

答案 0 :(得分:1)

To search for the position of a substring in pascal in a string, use the Pos() function.

In your case, the substring would consist of the name plus the # character.


A simple function to extract what comes after the name plus the # would look like:

function ExtractInfo( const searchName,data : String) : String;
var
 p : Integer;
begin
  p := Pos(searchName+'#',data); // Find position of name + '#' in data
  if (p > 0) then
    Result := Copy(data,p+Length(searchName)+1) // Copy after name and `#`
  else
    Result := '';
  // Note 1, if Result is not a valid way to assign the function result, 
  // use ExtractInfo instead.
  // Note 2, if only two parameters are not allowed in your pascal Copy function, 
  // add Length(data) as the third parameter.
end;

To test the function:

WriteLn(ExtractInfo('donald','donald#donald@hotmail.com'));