从C++中的字符串中获取特定值

huangapple go评论124阅读模式
英文:

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 = &quot;Count:0-&gt;&quot;;
  std::size_t pos = particleValueStart.find(countStr);
  if (pos == std::string::npos) {
    std::cout &lt;&lt; &quot;Count value not found&quot; &lt;&lt; std::endl;
    return 0;
  }

  pos += countStr.length();
  std::size_t endPos = particleValueStart.find(&#39;-&#39;, pos);
  if (endPos == std::string::npos) {
    std::cout &lt;&lt; &quot;Invalid count value&quot; &lt;&lt; std::endl;
    return 0;
  }

  std::string count = particleValueStart.substr(pos, endPos - pos);
  std::cout &lt;&lt; &quot;Count value is: &quot; &lt;&lt; count &lt;&lt; std::endl;

PS:-
If the number in Count:0-&gt; will dynamically change, you can modify this code to extract the number after -&gt; which is followed by Count:.

Here's the updated code:

std::string searchString = &quot;Count:&quot;;
    std::string::size_type dPos = particleValueStart.find(searchString);
    if (dPos != std::string::npos) {
        dPos += searchString.length(); // move the position to after &quot;Count:&quot;
        std::string::size_type arrowPos = particleValueStart.find(&quot;-&gt;&quot;, dPos);
        if (arrowPos != std::string::npos) {
            std::string count = particleValueStart.substr(arrowPos+2);
            int countValue = std::stoi(count);
            std::cout &lt;&lt; &quot;Count num is: &quot; &lt;&lt; countValue &lt;&lt; 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{&quot;---Start of list---\nSaved Value -&gt; Current Value\nlvlpin_dots: 0 -&gt; 2\nTwinkle2: 0 -&gt; 3\nlvlpin_10k_rays_smudge: 0 -&gt; 2\nfirst_attempt_tingling_004: 0 -&gt; 2\nboosterwheel_spinning_stars: 0 -&gt; 1\nActiveLevel3D: 0 -&gt; 1\nfirst_attempt_tingling_003: 0 -&gt; 2\nActiveLevel3D_2: 0 -&gt; 1\nlvlpin_10k_rays: 0 -&gt; 2\nActiveLevel3D_3: 0 -&gt; 1\nads_glitters_hears: 0 -&gt; 1\nlvlpin_10k_twinkles: 0 -&gt; 2\nads_dust_hearts: 0 -&gt; 1\nCount: 0 -&gt; 21\n---End of list---&quot;};
    std::string_view content{original};

    if (auto pos{content.find(&quot;Count&quot;)}; pos != std::string_view::npos)
    {                
        if ((pos = content.find_first_of(&#39;&gt;&#39;, 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 == &#39; &#39; &amp;&amp; digit_count == 0)
                    ++space_count;
                else break;
            }     

            content = content.substr(space_count, digit_count);
            std::cout &lt;&lt; content &lt;&lt; &#39;\n&#39;;             
        }        
    }
}

huangapple
  • 本文由 发表于 2023年3月9日 22:23:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定