我刚刚开始学习c ++,我在想他们是分裂字符串的方式。 Lemme让它变得更加清晰。
假设用户以下列格式dd-mm-yy
输入字符串,出生日期。现在我希望将日期,月份和年份存储在3个不同的变量中。那我该怎么办?
P.S :我用Google搜索了一下,发现可以使用boot::regex
完成此操作。但是,我仍然想知道是否有更简单的方法来做同样的事情。作为初学者会阻碍我。 :P
无论如何,任何帮助将不胜感激。
简要说明:
我想要这样的东西。
Enter date: //Accept the date
22-3-17 //Input by user
Date : 22 //Output
Month: 3 //Output
Year : 17 //Output
答案 0 :(得分:1)
您可以使用不同的方法。对于'最简单'的方法,我建议使用std :: string :: find()方法(http://www.cplusplus.com/reference/string/string/find/)与std :: string :: substr()方法(http://www.cplusplus.com/reference/string/string/substr/)
对于正则表达式的使用,您不需要提升:http://www.cplusplus.com/reference/regex/
答案 1 :(得分:1)
您可以使用sscanf功能: http://en.cppreference.com/w/cpp/io/c/fscanf
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
string date;
cin>>date;
int day, month, year;
sscanf(date.c_str(), "%d-%d-%d", &day, &month, &year);
cout << day << ' ' << month << ' ' << year;
return 0;
}
答案 2 :(得分:1)
您可以使用operator []来获取字符串的字符。
http://www.cplusplus.com/reference/string/string/operator[]/
答案 3 :(得分:0)
这是一个完整的工作程序,已在使用Visual Studio 2015社区版运行Windows 7 64位的英特尔酷睿2 Quad Extreme上编译,构建并成功运行,该版本已检索到OP在特定要求中提供的信息格式,适当地解析信息或数据的字符串,并以请求或要求的格式显示结果。
<强> stdafx.h中强>
#ifndef STDAFX_H
#define STDAFX_H
#include <vector>
#include <algorithm>
#include <iterator>
#include <tchar.h>
#include <conio.h>
#include <string>
#include <iostream>
#include <iomanip>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
<强> stdafx.cpp 强>
#include "stdafx.h"
<强> Utility.h 强>
#ifndef UTILITY_H
#define UTILITY_H
class Utility {
public:
static void pressAnyKeyToQuit();
static std::string toUpper( const std::string& str );
static std::string toLower( const std::string& str );
static std::string trim( const std::string& str, const std::string elementsToTrim = " \t\n\r" );
static unsigned convertToUnsigned( const std::string& str );
static int convertToInt( const std::string& str );
static float convertToFloat( const std::string& str );
static std::vector<std::string> splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true );
private:
Utility(); // Private - Not A Class Object
Utility( const Utility& c ); // Not Implemented
Utility& operator=( const Utility& c ); // Not Implemented
template<typename T>
static bool stringToValue( const std::string& str, T* pValue, unsigned uNumValues );
template<typename T>
static T getValue( const std::string& str, std::size_t& remainder );
}; // Utility
#include "Utility.inl"
#endif // UTILITY_H
<强> Utility.inl 强>
// ----------------------------------------------------------------------------
// stringToValue()
template<typename T>
static bool Utility::stringToValue(const std::string& str, T* pValue, unsigned uNumValues) {
int numCommas = std::count(str.begin(), str.end(), ',');
if (numCommas != uNumValues - 1) {
return false;
}
std::size_t remainder;
pValue[0] = getValue<T>(str, remainder);
if (uNumValues == 1) {
if (str.size() != remainder) {
return false;
}
}
else {
std::size_t offset = remainder;
if (str.at(offset) != ',') {
return false;
}
unsigned uLastIdx = uNumValues - 1;
for (unsigned u = 1; u < uNumValues; ++u) {
pValue[u] = getValue<T>(str.substr(++offset), remainder);
offset += remainder;
if ((u < uLastIdx && str.at(offset) != ',') ||
(u == uLastIdx && offset != str.size()))
{
return false;
}
}
}
return true;
} // stringToValue
<强> Utility.cpp 强>
#include "stdafx.h"
#include "Utility.h"
// ----------------------------------------------------------------------------
// pressAnyKeyToQuit()
void Utility::pressAnyKeyToQuit() {
std::cout << "Press any key to quit" << std::endl;
_getch();
} // pressAnyKeyToQuit
// ----------------------------------------------------------------------------
// toUpper()
std::string Utility::toUpper(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::toupper);
return result;
} // toUpper
// ----------------------------------------------------------------------------
// toLower()
std::string Utility::toLower(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::tolower);
return result;
} // toLower
// ----------------------------------------------------------------------------
// trim()
// Removes Elements To Trim From Left And Right Side Of The str
std::string Utility::trim(const std::string& str, const std::string elementsToTrim) {
std::basic_string<char>::size_type firstIndex = str.find_first_not_of(elementsToTrim);
if (firstIndex == std::string::npos) {
return std::string(); // Nothing Left
}
std::basic_string<char>::size_type lastIndex = str.find_last_not_of(elementsToTrim);
return str.substr(firstIndex, lastIndex - firstIndex + 1);
} // trim
// ----------------------------------------------------------------------------
// getValue()
template<>
float Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stof(str, &remainder);
} // getValue <float>
// ----------------------------------------------------------------------------
// getValue()
template<>
int Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoi(str, &remainder);
} // getValue <int>
// ----------------------------------------------------------------------------
// getValue()
template<>
unsigned Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoul(str, &remainder);
} // getValue <unsigned>
// ----------------------------------------------------------------------------
// convertToUnsigned()
unsigned Utility::convertToUnsigned(const std::string& str) {
unsigned u = 0;
if (!stringToValue(str, &u, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to unsigned";
throw strStream.str();
}
return u;
} // convertToUnsigned
// ----------------------------------------------------------------------------
// convertToInt()
int Utility::convertToInt(const std::string& str) {
int i = 0;
if (!stringToValue(str, &i, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to int";
throw strStream.str();
}
return i;
} // convertToInt
// ----------------------------------------------------------------------------
// convertToFloat()
float Utility::convertToFloat(const std::string& str) {
float f = 0;
if (!stringToValue(str, &f, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to float";
throw strStream.str();
}
return f;
} // convertToFloat
// ----------------------------------------------------------------------------
// splitString()
std::vector<std::string> Utility::splitString( const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty ) {
std::vector<std::string> vResult;
if ( strDelimiter.empty() ) {
vResult.push_back( strStringToSplit );
return vResult;
}
std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
while ( true ) {
itSubStrEnd = search( itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end() );
std::string strTemp( itSubStrStart, itSubStrEnd );
if ( keepEmpty || !strTemp.empty() ) {
vResult.push_back( strTemp );
}
if ( itSubStrEnd == strStringToSplit.end() ) {
break;
}
itSubStrStart = itSubStrEnd + strDelimiter.size();
}
return vResult;
} // splitString
<强>的main.cpp 强>
#include "stdafx.h"
#include "Utility.h"
int main () {
std::string date;
std::cout << "Enter Date in DD-MM-YY Format.\n" << std::endl;
std::getline( std::cin, date );
std::vector<std::string> vResults = Utility::splitString( date, "-" );
std::cout << "\nDate : " << vResults[0] << std::endl
<< "Month: " << vResults[1] << std::endl
<< "Year : " << vResults[2] << std::endl << std::endl;
Utility::pressAnyKeyToQuit();
return 0;
} // main