当前位置: 首页>>代码示例>>C++>>正文


C++ String::substr方法代码示例

本文整理汇总了C++中sid::String::substr方法的典型用法代码示例。如果您正苦于以下问题:C++ String::substr方法的具体用法?C++ String::substr怎么用?C++ String::substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sid::String的用法示例。


在下文中一共展示了String::substr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: substrTest

void StringTestCase::substrTest()
{
    // Sunny day
    {
    	Sid::String str("foobajooba");
    	Sid::String sub = str.substr(0,0);

    	CPPUNIT_ASSERT_PRINTF( sub == "f",
    				"Basic substr logic is broken (input was %s, 0, 0)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(0, 9);
    	CPPUNIT_ASSERT_PRINTF( sub == "foobajooba",
    				"Basic substr logic is broken (input was %s, 0, 9)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(0, 8);
    	CPPUNIT_ASSERT_PRINTF( sub == "foobajoob",
    				"Basic substr logic is broken (input was %s, 0, 8)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(1, 9);
    	CPPUNIT_ASSERT_PRINTF( sub == "oobajooba",
    				"Basic substr logic is broken (input was %s, 1, 9)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(1, 8);
    	CPPUNIT_ASSERT_PRINTF( sub == "oobajoob",
    				"Basic substr logic is broken (input was %s, 1, 8)\nSubstring was %s\n",
    				str.data(),
    				sub.data());
    }

    // Null string
    {
    	Sid::String str;
    	Sid::String sub = str.substr(0,0);
    	CPPUNIT_ASSERT_MESSAGE("Substring (0,0) of a null string somehow is not null. Fascinating!\n",
    				sub.isNull());

    	sub = str.substr(0, -1);
    	CPPUNIT_ASSERT_MESSAGE( "Substring (0,-1) of a null string somehow is not null. Fascinating!\n",
    				sub.isNull());
    }

    // Empty string
    {
    	Sid::String str("");
    	Sid::String sub = str.substr(0,0);
    	CPPUNIT_ASSERT_MESSAGE("Substring (0,0) of an empty string somehow is not empty. Fascinating!\n",
    				sub.isEmpty() && !sub.isNull());

    	sub = str.substr(0, -1);
    	CPPUNIT_ASSERT_MESSAGE( "Substring (0,-1) of an empty string somehow is not empty. Fascinating!\n",
    				sub.isEmpty() && !sub.isNull());
    }

    // boundary fixup
    {
    	Sid::String str("foobajooba");
    	Sid::String sub = str.substr(0, -1);
    	CPPUNIT_ASSERT_PRINTF( sub == "foobajoob",
    				"Negative index code is somehow broken (input was %s, 0, -1)\nSubstring was %s\n",
    				str.data(), 
    				sub.data());

    	sub = str.substr(0, -9);

    	CPPUNIT_ASSERT_PRINTF( sub == "f",
    				"Negative index code is somehow broken (input was %s, 0, -9)\nSubstring was %s\n",
    				str.data(), 
    				sub.data());

    	sub = str.substr(0, -9999999);
    	CPPUNIT_ASSERT_PRINTF( sub == "f",
    			"Negative index modulus code is somehow broken (input was %s, 0, -9999999)\nSubstring was %s\n",
    			str.data(), 
    			sub.data());

    	sub = str.substr(-5, 2);
    	CPPUNIT_ASSERT_PRINTF( sub == "foo",
    				"Negative leading bound fixup seems to not be working (input was %s, -5, 2)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(5, 2);
    	CPPUNIT_ASSERT_PRINTF( sub == "obaj",
    				"Bound swapping fixup seems to not be working (input was %s, 5, 2)\nSubstring was %s\n",
    				str.data(),
    				sub.data());

    	sub = str.substr(5, 900);
    	CPPUNIT_ASSERT_PRINTF( sub == "jooba",
    				"Large upper bound fixup seems to not be working (input was %s, 5, 900)\nSubstring was %s\n",
    				str.data(),
    				sub.data());
//.........这里部分代码省略.........
开发者ID:nosilence,项目名称:jira-skype-app,代码行数:101,代码来源:test_string.cpp


注:本文中的sid::String::substr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。