如何获取在字符串中重复多次的字符串的特定实例 [英] How do I get a particular instance of a string that repeats multiple times in a string

查看:68
本文介绍了如何获取在字符串中重复多次的字符串的特定实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串KeyData中的数据(其存在的是< eg2keydata>属性)转换为*。但它总是在字符串msg中获取keyData的第一个实例。如何在每次转换时获取特定实例。 (或)有没有一种方法,find命令提供所有实例,而不仅仅是第一次出现的字符串。



我尝试过:



  const  std :: string& msg =   177 \>< eg1KeyData Protocol = \abc \KeyLength = \ 6 \KeyData = \d15444 \>< / eg1KeyData>< eg2KeyData Protocol = \hm \KeyLength = \6 \KeyData = \432579 \ >< / eg2KeyData>< Tunne; 
std :: size_t search1 = msg.find( < eg2KeyData);
const std: :string from1 = KeyData = \;
const std :: string to1 = \>< / eg2KeyData>;
std :: size_t endpos1 = msg.find(to1);
std :: string s = msg;
std :: size_t l1 = 0 ;
if (msg.find( < eg2KeyData)!= std :: string :: npos)
{
for (search1; search1< endpos1; search1 ++)
{
l1 = msg.find( KeyData = \ );
}
如果(l1!= 0
{
std :: size_t r1 = endpos1-l1-from1.length();
s.replace(l1 + from1.length(),r1,r1,' *');
}
}
std :: cout<< s;

解决方案

  if (msg.find(  < eg2Data)!= std :: string :: npos)



源文件中不存在该文本。


I want to convert the data in the string KeyData(which is present is <eg2keydata> attribute) to *.But it always takes the first instance of keyData in the string msg. How can I get the particular instance everytime I want to convert. (or) Is there a way where find command gives all the instances instead of just the first occurance of a string.

What I have tried:

 const std::string& msg = "177\"><eg1KeyData Protocol=\"abc\" KeyLength=\"6\" KeyData=\"d15444\"></eg1KeyData><eg2KeyData Protocol=\"hm\" KeyLength=\"6\" KeyData=\"432579\"></eg2KeyData><Tunne ";
         std::size_t search1 =msg.find("<eg2KeyData"); 
         const std::string from1 = "KeyData=\"";        
         const std::string to1= "\"></eg2KeyData>";
         std::size_t endpos1 = msg.find(to1);
         std::string s=msg;
         std::size_t l1=0;      
         if (msg.find("<eg2KeyData")!=std::string::npos)
{  
         for(search1; search1<endpos1;search1++)
{
         l1=msg.find("KeyData=\"");            
}
        if(l1!=0)
{        
        std::size_t r1= endpos1-l1-from1.length();
        s.replace(l1+from1.length(),r1,r1,'*');        
 }
}
     std::cout<<s;

解决方案

if (msg.find("<eg2Data")!=std::string::npos)


That text does not exist in your source string.


这篇关于如何获取在字符串中重复多次的字符串的特定实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆