用C两个特定的字符串之间提取字符串 [英] Extract string between two specific strings in C
本文介绍了用C两个特定的字符串之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你如何提取两个指定的字符串之间字符串?
例如:<标题>提取该< /标题>
。有没有一种简单的方法用得到它的strtok()
或任何更简单呢?
How do you extract strings between two specified strings?
For Example:
<title>Extract this</title>
. Is there a simple way to get it using strtok()
or anything simpler?
编辑:两个指定的字符串是&LT;标题&GT;
和&LT; /标题&GT;
和字符串提取为提取该
。
The two specified strings are <title>
and </title>
and the string extracted is Extract this
.
推荐答案
- 搜索使用第一个子字符串
的strstr()
。 - 如果找到,保存子字符串数组索引
- 从那里,寻找下一个子字符串。
- 如果找到,
之间的所有内容
和[子字符串的开始[子串1] + [子字符串的长度为1]的开始] 2]
是你感兴趣的字符串。 - 提取使用字符串
函数strncpy()
或的memcpy()
。
- Search for the first sub string using
strstr()
. - If found, save the array index of the sub string
- From there, search for the next sub string.
- If found, everything between
[ [start of sub string 1] + [length of sub string 1] ]
and[start of sub string 2]
is the string you are interested in. - Extract the string using
strncpy()
ormemcpy()
.
这篇关于用C两个特定的字符串之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文