本文整理汇总了C++中std::basic_string::substr方法的典型用法代码示例。如果您正苦于以下问题:C++ basic_string::substr方法的具体用法?C++ basic_string::substr怎么用?C++ basic_string::substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::basic_string
的用法示例。
在下文中一共展示了basic_string::substr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: kbe_split
inline void kbe_split(const std::basic_string<T>& s, T c, std::vector< std::basic_string<T> > &v)
{
if(s.size() == 0)
return;
typename std::basic_string< T >::size_type i = 0;
typename std::basic_string< T >::size_type j = s.find(c);
while(j != std::basic_string<T>::npos)
{
std::basic_string<T> buf = s.substr(i, j - i);
if(buf.size() > 0)
v.push_back(buf);
i = ++j;
j = s.find(c, j);
}
if(j == std::basic_string<T>::npos)
{
std::basic_string<T> buf = s.substr(i, s.length() - i);
if(buf.size() > 0)
v.push_back(buf);
}
}
示例2: split_by_tokens
ResultList split_by_tokens(const std::basic_string<CharT> &s,
const std::basic_string<CharT> &tokens, bool keepEmptyParts = true)
{
if (tokens.length() == 1)
return split<ResultList>(s, tokens, keepEmptyParts);
ResultList slist;
typename std::basic_string<CharT>::size_type start = 0;
typename std::basic_string<CharT>::size_type pos;
std::basic_string<CharT> part;
// If delimiter.empty():
// pos = s.find_first_of(delimiter, start);
// pos will be s.npos.
while ((pos = s.find_first_of(tokens, start)) != s.npos) { // strtok
part = s.substr(start, pos - start);
if (!part.empty() || keepEmptyParts)
slist.push_back(part);
start = pos + 1;
}
if (start != s.length() || keepEmptyParts)
slist.push_back(s.substr(start));
return slist;
}
示例3: if
std::vector< std::basic_string< CharType > > str_split( std::basic_string< CharType > const & p_str, std::basic_string< CharType > const & p_delims, uint32_t p_maxSplits, bool p_bKeepVoid )
{
typedef std::basic_string< CharType > string_t;
std::vector< string_t > l_arrayReturn;
if ( ! p_str.empty() && ! p_delims.empty() && p_maxSplits > 0 )
{
l_arrayReturn.reserve( p_maxSplits + 1 );
std::size_t l_numSplits = 0;
std::size_t l_pos = 0;
std::size_t l_start = 0;
do
{
l_pos = p_str.find_first_of( p_delims, l_start );
if ( l_pos == l_start )
{
l_start = l_pos + 1;
if ( p_bKeepVoid )
{
l_arrayReturn.push_back( string_t() );
}
}
else if ( l_pos == string_t::npos || l_numSplits == p_maxSplits )
{
string_t remnants = p_str.substr( l_start );
if ( !remnants.empty() || p_bKeepVoid )
{
l_arrayReturn.push_back( remnants );
}
return l_arrayReturn;
}
else
{
l_arrayReturn.push_back( p_str.substr( l_start, l_pos - l_start ) );
l_start = l_pos + 1;
}
//l_start = p_str.find_first_not_of( p_delims, l_start );
++ l_numSplits;
}
while ( l_pos != string_t::npos );
}
return l_arrayReturn;
}
示例4: split
ResultList split(const std::basic_string<CharT> &s,
const std::basic_string<CharT> &delimiter, bool keepEmptyParts = true)
{
ResultList slist;
// If delimiter.empty():
// pos = s.find(delimiter, start);
// pos will be 0.
if (delimiter.empty()) {
slist.push_back(s);
return slist;
}
typename std::basic_string<CharT>::size_type start = 0;
typename std::basic_string<CharT>::size_type pos;
std::basic_string<CharT> part;
if (delimiter.length() == 1) {
CharT ch = delimiter[0];
// Hope that:
// find(std::basic_string<CharT>, CharT ch)
// will be faster than:
// find(std::basic_string<CharT>, std::basic_string<CharT>)
while ((pos = s.find(ch, start)) != s.npos) { // Use strchr/wcschr instead?
part = s.substr(start, pos - start);
if (!part.empty() || keepEmptyParts)
slist.push_back(part);
start = pos + delimiter.length();
}
} else {
while ((pos = s.find(delimiter, start)) != s.npos) { // Use strstr/wcsstr instead?
part = s.substr(start, pos - start);
if (!part.empty() || keepEmptyParts)
slist.push_back(part);
start = pos + delimiter.length();
}
}
if (start != s.length() || keepEmptyParts)
slist.push_back(s.substr(start));
return slist;
}
示例5: new_text
sf::Vector2f FeTextPrimative::setString(
const std::basic_string<sf::Uint32> &t,
int position )
{
//
// Cut string if it is too big to fit our dimension
//
int first_char, len, disp_cpos;
if ( m_wrap ) position = 0;
fit_string( t, position, first_char, len );
m_texts[0].setString( t.substr( first_char, len ) );
disp_cpos = position - first_char;
if ( m_texts.size() > 1 )
m_texts.resize( 1 );
if (( m_wrap ) && ( len < (int)t.size() ))
{
//
// Calculate the number of lines we can fit in our RectShape
//
unsigned int spacing = getCharacterSize() * m_texts[0].getScale().y;
const sf::Font *font = getFont();
if ( font )
spacing = font->getLineSpacing( spacing );
sf::FloatRect rectSize = m_bgRect.getLocalBounds();
int line_count = rectSize.height / spacing;
//
// Create the wrapped lines
//
position = len;
int i=0;
while (( position < (int)t.size() - 1 ) && ( i < line_count ))
{
fit_string( t, position, first_char, len );
sf::Text new_text( m_texts[0] );
new_text.setString( t.substr( first_char, len ) );
position += len;
m_texts.push_back( new_text );
i++;
}
}
set_positions(); // we need to set the positions now for findCharacterPos() to work below
return m_texts[0].findCharacterPos( disp_cpos );
}
示例6:
std::basic_string<CharType> parent_path(const std::basic_string<CharType>& path)
{
auto index = path.size();
if (index)
{
auto str = path.c_str();
for (--index; index > 0; --index)
{
auto c = str[index];
if (c != '\\' && c != '/')
break;
}
for (--index; index > 0; --index)
{
auto c = str[index];
if (c == '\\' || c == '/')
break;
}
}
return index ? path.substr(0, index + 1) : std::basic_string<CharType>();
}
示例7: benchmarkT
void benchmarkT(const char *msg1, F1 f1, const char *msg2, F2 f2, const std::basic_string<C>& str, const std::basic_string<C>& key)
{
Ret r1 = benchmark1(str, key, f1);
Ret r2 = benchmark1(str, key, f2);
printf("%25s %16s % 6.2f %16s % 6.2f %5.2f\n", u16tos(key.substr(0, 25)).c_str(), msg1, r1.clk, msg2, r2.clk, r1.clk / r2.clk);
TEST_EQUAL(r1, r2);
}
示例8: PathRemoveFilename
std::basic_string<Char> PathRemoveFilename(const std::basic_string<Char>& path)
{
std::basic_string<Char>::size_type nLastSlash = 0;
nLastSlash = StringFindLastOf(path, "\\/");
if(nLastSlash == std::string::npos) return path;
return path.substr(0, nLastSlash);
}
示例9: lstrip
static inline std::basic_string<_CharT, _Traits, _Alloc> lstrip(
const std::basic_string<_CharT, _Traits, _Alloc> &a,
const strip_t stripchars) {
typedef std::basic_string<_CharT, _Traits, _Alloc> str_t;
typename str_t::size_type p = a.find_first_not_of(stripchars);
if (p == str_t::npos) return "";
return a.substr(p);
}
示例10: Split
std::vector<std::basic_string<TCHAR>> Split (const std::basic_string<TCHAR> &inString,
const std::basic_string<TCHAR> &separator)
{
std::vector<std::basic_string<TCHAR>> returnVector;
std::basic_string<TCHAR>::size_type start = 0;
std::basic_string<TCHAR>::size_type end = 0;
while ((end=inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}
returnVector.push_back (inString.substr (start));
return returnVector;
}
示例11: rstrip
static inline std::basic_string<_CharT, _Traits, _Alloc> rstrip(
const std::basic_string<_CharT, _Traits, _Alloc> &a) {
typedef std::basic_string<_CharT, _Traits, _Alloc> str_t;
typename str_t::size_type p = a.size();
std::locale loc;
while (p && std::isspace(a[p-1], loc)) p--;
if (!p) return "";
return a.substr(0, p);
}
示例12: node_select_piece
explicit node_select_piece(const std::basic_string<char_type>& str)
: tag(), type(specifier_type::none_), attr()
{
auto f1 = std_future::overload(
[](const std::string& str) { return str.find_first_of("#."); },
[](const std::wstring& str) { return str.find_first_of(L"#."); }
);
const auto split_pos = f1(str);
if (basic_string<char_type>::npos == split_pos) {
tag = str;
}
else {
auto is_sharp = std_future::overload(
[](const char c) { return '#' == c; },
[](const wchar_t c) { return L'#' == c; }
);
tag = str.substr(0, split_pos);
type = (is_sharp(str[split_pos])) ? specifier_type::id_ : specifier_type::class_;
attr = str.substr(split_pos + 1);
}
}
示例13: PathIsAbsolute
inline bool PathIsAbsolute(const std::basic_string<Char>& path)
{
bool r = false;
if(path.length() >= 3)
{
std::basic_string<Char> sSearch = path.substr(1, 2);
if(StringEquals(sSearch, ":/") || StringEquals(sSearch, ":\\"))
{
r = true;
}
}
return r;
}
示例14: draw
void CursesOut::draw(const Position &pos, const Style &stl, const std::basic_string<char> &text) const {
if ((int) text.length() > pos.w) {
auto s = text.substr(0, pos.w);
mvaddstr(pos.textY, pos.textX, s.c_str());
return;
}
mvaddstr(pos.textY, pos.textX, text.c_str());
if ((int) text.length() < pos.w) {
std::basic_string<char> s;
s.append(pos.w - text.length(), ' ');
mvaddstr(pos.textY, pos.textX + text.length(), s.c_str());
}
}
示例15: replace
int replace(std::basic_string<Ch, Tr, Alloc>& target,
const std::basic_string<Ch, Tr, Alloc>& oldStr,
const std::basic_string<Ch, Tr, Alloc>& newStr)
{
int replaceCount = 0;
typedef std::basic_string<Ch, Tr, Alloc> str_type;
str_type::size_type searchPos = 0;
str_type::size_type findPos = 0;
str_type result;
for(;;)
{
findPos = StringUtil::find(target, oldStr, searchPos);
if(str_type::npos == findPos)
{
break;
}
result += target.substr(searchPos, (findPos - searchPos));
result += newStr;
searchPos = findPos + oldStr.length();
++replaceCount;
}
if(searchPos < target.length())
{
result += target.substr(searchPos);
}
target = result;
return replaceCount;
}