英文:
Get specific value from a String in C++
问题
std::string::size_type dPos = particleValueStart.find("Count:");
std::string count = particleValueStart.substr(dPos + 6);
Log("Count num is: %s", count.c_str());
英文:
I have the following string:
"---Start of list---\nSaved Value -> Current Value\nlvlpin_dots: 0 -> 2\nTwinkle2: 0 -> 3\nlvlpin_10k_rays_smudge: 0 -> 2\nfirst_attempt_tingling_004: 0 -> 2\nboosterwheel_spinning_stars: 0 -> 1\nActiveLevel3D: 0 -> 1\nfirst_attempt_tingling_003: 0 -> 2\nActiveLevel3D_2: 0 -> 1\nlvlpin_10k_rays: 0 -> 2\nActiveLevel3D_3: 0 -> 1\nads_glitters_hears: 0 -> 1\nlvlpin_10k_twinkles: 0 -> 2\nads_dust_hearts: 0 -> 1\nCount: 0 -> 21\n---End of list---"
Then I removed the whitespace and got this:
---Startoflist---SavedValue->CurrentValuelvlpin_dots:0->2Twinkle2:0->3lvlpin_10k_rays_smudge:0->2first_attempt_tingling_004:0->2boosterwheel_spinning_stars:0->1ActiveLevel3D:0->1first_attempt_tingling_003:0->2ActiveLevel3D_2:0->1lvlpin_10k_rays:0->2ActiveLevel3D_3:0->1ads_glitters_hears:0->1lvlpin_10k_twinkles:0->2ads_dust_hearts:0->1Count:0->21---Endoflist---
I want to get the number for Count
, which appears at the end of the string, Count:0->21
, and I only want to have the number 21.
Count:0-> this number 0 will dynamically change, for example first will be Count:0-> 21, then next time will be Count:21->somenumber ,
I tried the following way, but it doesn't give the result I want.
std::string::size_type dPos = particleValueStart.find("Count");
std::string count = particleValueStart.substr (dPos);
Log("Count num is: %s", count);
答案1
得分: 0
你可以使用 find()
方法来查找第一个字符在 "Count:0->" 中的索引。
std::string countStr = "Count:0->";
std::size_t pos = particleValueStart.find(countStr);
if (pos == std::string::npos) {
std::cout << "未找到计数值" << std::endl;
return 0;
}
pos += countStr.length();
std::size_t endPos = particleValueStart.find('-', pos);
if (endPos == std::string::npos) {
std::cout << "无效的计数值" << std::endl;
return 0;
}
std::string count = particleValueStart.substr(pos, endPos - pos);
std::cout << "计数值为: " << count << std::endl;
附注:
如果 "Count:0->" 中的数字将动态更改,您可以修改此代码以提取 "Count:" 后面的 "->" 后面的数字。
以下是更新后的代码:
std::string searchString = "Count:";
std::string::size_type dPos = particleValueStart.find(searchString);
if (dPos != std::string::npos) {
dPos += searchString.length(); // 将位置移动到 "Count:" 之后
std::string::size_type arrowPos = particleValueStart.find("->", dPos);
if (arrowPos != std::string::npos) {
std::string count = particleValueStart.substr(arrowPos + 2);
int countValue = std::stoi(count);
std::cout << "计数数值为: " << countValue << std::endl;
}
}
英文:
you can use find()
to find the index of 1st character in "Count:0->"
std::string countStr = "Count:0->";
std::size_t pos = particleValueStart.find(countStr);
if (pos == std::string::npos) {
std::cout << "Count value not found" << std::endl;
return 0;
}
pos += countStr.length();
std::size_t endPos = particleValueStart.find('-', pos);
if (endPos == std::string::npos) {
std::cout << "Invalid count value" << std::endl;
return 0;
}
std::string count = particleValueStart.substr(pos, endPos - pos);
std::cout << "Count value is: " << count << std::endl;
PS:-
If the number in Count:0->
will dynamically change, you can modify this code to extract the number after ->
which is followed by Count:
.
Here's the updated code:
std::string searchString = "Count:";
std::string::size_type dPos = particleValueStart.find(searchString);
if (dPos != std::string::npos) {
dPos += searchString.length(); // move the position to after "Count:"
std::string::size_type arrowPos = particleValueStart.find("->", dPos);
if (arrowPos != std::string::npos) {
std::string count = particleValueStart.substr(arrowPos+2);
int countValue = std::stoi(count);
std::cout << "Count num is: " << countValue << std::endl;
}
}
答案2
得分: 0
你可以使用std::string_view
以高效的方式解析string
(无需分配内存)。然后,你可以使用std::string_view
上定义的一些成员函数来帮助定位你感兴趣的子字符串。
std::string_view
本质上是字符串或字符串部分的readonly
窗口。请注意,使用std::string_view
不能修改基础字符串,因为它只允许查看内容而不允许修改它。
int main()
{
const std::string original{"---开始列表---\nSaved Value -> Current Value\nlvlpin_dots: 0 -> 2\nTwinkle2: 0 -> 3\nlvlpin_10k_rays_smudge: 0 -> 2\nfirst_attempt_tingling_004: 0 -> 2\nboosterwheel_spinning_stars: 0 -> 1\nActiveLevel3D: 0 -> 1\nfirst_attempt_tingling_003: 0 -> 2\nActiveLevel3D_2: 0 -> 1\nlvlpin_10k_rays: 0 -> 2\nActiveLevel3D_3: 0 -> 1\nads_glitters_hears: 0 -> 1\nlvlpin_10k_twinkles: 0 -> 2\nads_dust_hearts: 0 -> 1\nCount: 0 -> 21\n---结束列表---"};
std::string_view content{original};
if (auto pos{content.find("Count")}; pos != std::string_view::npos)
{
if ((pos = content.find_first_of('>', pos)) != std::string_view::npos)
{
content = content.substr(pos + 1);
std::size_t digit_count = 0;
std::size_t space_count = 0;
for (const char c : content)
{
if (std::isdigit(c))
++digit_count;
else if (c == ' ' && digit_count == 0)
++space_count;
else break;
}
content = content.substr(space_count, digit_count);
std::cout << content << '\n';
}
}
}
英文:
You can parse the string
in an efficient way (without having to allocate) using std::string_view
.
You can then use some of the member functions defined on the std::string_view
to help you locate the sub string you're interested in.
std::string_view
is essentially a readonly
window into a string or part of a string. Note that you cannot modify the underlying string using a std::string_view
as it only allows viewing the content but not modifying it.
int main()
{
const std::string original{"---Start of list---\nSaved Value -> Current Value\nlvlpin_dots: 0 -> 2\nTwinkle2: 0 -> 3\nlvlpin_10k_rays_smudge: 0 -> 2\nfirst_attempt_tingling_004: 0 -> 2\nboosterwheel_spinning_stars: 0 -> 1\nActiveLevel3D: 0 -> 1\nfirst_attempt_tingling_003: 0 -> 2\nActiveLevel3D_2: 0 -> 1\nlvlpin_10k_rays: 0 -> 2\nActiveLevel3D_3: 0 -> 1\nads_glitters_hears: 0 -> 1\nlvlpin_10k_twinkles: 0 -> 2\nads_dust_hearts: 0 -> 1\nCount: 0 -> 21\n---End of list---"};
std::string_view content{original};
if (auto pos{content.find("Count")}; pos != std::string_view::npos)
{
if ((pos = content.find_first_of('>', pos)) != std::string_view::npos)
{
content = content.substr(pos + 1);
std::size_t digit_count = 0;
std::size_t space_count = 0;
for (const char c : content)
{
if (std::isdigit(c))
++digit_count;
else if (c == ' ' && digit_count == 0)
++space_count;
else break;
}
content = content.substr(space_count, digit_count);
std::cout << content << '\n';
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论