使用2个定界符从文本文件中提取字符串 [英] Extract string from a text file using 2 delimiters
本文介绍了使用2个定界符从文本文件中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用2个定界符从文本文件中提取字符串。
I'm trying to extract a string from a text file using 2 delimiters. One to start and one to stop.
示例:
Hi my name is$John and I'm happy/today
我需要做的是该函数将返回 $
和 /
之间的字符串。我到处都是,但是似乎找不到有用的东西,而且我还是编程新手。
What I need to do is to call a function that would return the string between $
and /
. I've been looking everywhere but I can't seem to find something useful and I'm new to programming.
推荐答案
您可以使用 Pos
和 Copy
做到这一点:
You can do it with Pos
and Copy
:
function ExtractText(const Str: string; const Delim1, Delim2: char): string;
var
pos1, pos2: integer;
begin
result := '';
pos1 := Pos(Delim1, Str);
pos2 := Pos(Delim2, Str);
if (pos1 > 0) and (pos2 > pos1) then
result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
end;
这篇关于使用2个定界符从文本文件中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文