一个截取字符串函数,参数列表(源字符串,开始字符串,结束字符串)

经常用,不想再费脑筋

function TForm1.GetStr(StrSource, StrBegin, StrEnd: string): string;
var
 in_star,in_end:integer;
 strtmp:string;
begin
  in_star:=AnsiPos(strbegin,strsource)+length(strbegin);
  strtmp:=copy(strsource,in_star,length(strsource));
  in_end:=AnsiPos(strend,strtmp);
  result:=copy(strsource,in_star,in_end-1);
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,可以使用多种方法来截取字符串。以下是一些常用的方法: 1. substr()函数:substr()函数可以从一个字符串中提取子字符串。它接受两个参数,第一个参数是起始位置,第二个参数是要提取的子字符串的长度。示例代码如下: ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; std::string sub = str.substr(7, 5); // 从位置7开始提取长度为5的子字符串 std::cout << sub << std::endl; // 输出 "World" return 0; } ``` 2. getline()函数:getline()函数可以从一个字符串中按照指定的分隔符提取子字符串。示例代码如下: ```cpp #include <iostream> #include <string> #include <sstream> int main() { std::string str = "Hello, World!"; std::istringstream iss(str); std::string sub; std::getline(iss, sub, ','); // 按照逗号分隔提取子字符串 std::cout << sub << std::endl; // 输出 "Hello" return 0; } ``` 3. find()和substr()函数的组合:可以使用find()函数找到指定子字符串的位置,然后再使用substr()函数截取子字符串。示例代码如下: ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; size_t pos = str.find(","); // 找到逗号的位置 std::string sub = str.substr(0, pos); // 从开头截取到逗号位置的子字符串 std::cout << sub << std::endl; // 输出 "Hello" return 0; } ``` 这些是C++中常用的截取字符串的方法。你可以根据具体的需求选择适合的方法来截取字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值