在C 中打破字符串

Breaking String in c++

本文关键字:字符串      更新时间:2023-10-16

我试图将字符串在不同的块中打破字符串,并将这些块存储在字符串数组中以进行进一步实现,但我遇到了一些问题。例如,当我输入172.16.524.1时,它像172.16.524.0一样存储这是我的代码:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
   string address;
   string str1[4];
   int found,i=0,n1[4],n2,n3,n4;
   cin>>address;
   for(int a=0 ; a<address.length() ; a++)
   {
       found=address.find(".");
       for(int f=0 ; f<found ; f++)
       {
           str1[i]+=address[f];
       }
       i++;
       address.erase(0,found+1);
   }
   for(int j=0 ; j<i ; j++)
   {
       n1[j]=atoi(str1[j].c_str()); //To convert String int to integer
       cout<<n1[j]<<" ";
   }
   cout<<endl;
   return 0;
}

您将旧式的C字符串与新样式C 字符串

混合
   found=address.find(".");
   str1[i] = address.substr(0, found) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 
   i++ ;

应该做您想做的事,但是使用std :: vector可能会有所帮助,因此以下是一个整洁的

   std::vector<std::string> str1 ;
   ... 
   found=address.find(".");
   str1.push_back(address.substr(0, found)) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 
std::string s = "172.168.1.15";
std::string delimiter = ".";
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

解析字符串的标准方式请参阅此问题

我在其中有一些问题

我更喜欢std :: stringstream。std :: getline()的第三参数是一个定界符值,似乎非常适合此挑战。

在以下代码中,我将每个字段提取为字符串,然后将其推入字符串的向量(addrfields)。

要报告结果,我列出了字段,然后以使用STD :: stoi()的字段再次输出输出。

注意:我附加了'。'。到达地址的特定器...这微不足道避免了eof()截断了最后一个字段。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>
int exec()
{
   std::string address ("172.16.524.1");
   std::cout << "nn  echo of input:     '" << address << "'" << std::endl;
   std::vector<std::string> addrFields; 
   {
      // fill ss with the address    vvv  append one delim
      std::stringstream ss(address + '.');
      std::cout << "  ss contents:       '" << ss.str() 
                << "'" << std::endl;
      do // loop through fields
      {
         std::string f;
         (void)std::getline(ss, f, '.'); // each field ends at delim
         if (false == ss.good())
         {
            if (ss.eof()) break;  // quietly exit, a normal op
            // something wrong with input
            std::cerr << "n  err Line: " << address << std::endl;
            assert(0); // no use continuing with bad input
         }
         if(f.size()) // ignore blank fields
            addrFields.push_back(f);
         // consider using addrFields.size() 
         //    for special handling 4 or 6 fields
         //    for special handling of a colon appended port num
      } while(true);
   }
   std::cout << "n  Results  " << std::endl;
   std::cout << "n      addrFields :   ";
   for (auto f : addrFields)
      std::cout << f << "   ";
   std::cout << "  (strings)   "
             << "nn       addr ints :   ";
   for (auto f : addrFields)
      std::cout << std::stoi(f) << "   ";
   std::cout << "  (converted to ints) " << std::endl;
   return 0;
}

输出是:

  echo of input:     '172.16.524.1'
  ss contents:       '172.16.524.1.'
  Results  
      addrFields :   172   16   524   1     (strings)   
       addr ints :   172   16   524   1     (converted to ints)