substr 函数

本文详细介绍了 C++ 中 basic_string::substr 函数的使用方法,包括如何从指定位置复制子字符串及其参数设置。通过具体示例展示了不同参数组合的效果,并对比了 C++ 与 Oracle 中 substr 的使用差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

substr

编辑本段定义和用法

  basic_string::substr
  basic_string substr( size_type _Off = 0,size_type _Count = npos) const;
  功能:从一个字符串复制一个从指定位置开始,并具有指定长度的子字符串。
  参数
  _Off
  所需的子字符串的起始位置。字符串中第一个字符的索引为 0,默认值为0.
  _Count
  复制的字符数目
  返回值
  一个子字符串,从其指定的位置开始

编辑本段备注

  如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。

编辑本段示例

  下面的示例阐释了 substr 方法的用法。
  function SubstrDemo(){
  var s, ss; //Declare variables.
  var s = "The rain in Spain falls mainly in the plain.";
  ss = s.substr(12, 5); //Get substring.
  return(ss); // Returns "Spain".
  ----------------------------------------------crazyghost_von补充-----------------------------------------------------------------------
  s.substr(12)的结果是 Spain falls mainly in the plain.
  ----------------------------------------------------------------------------------------------------------------------------------------------
  Code : C++中 的代码如下
  // basic_string_substr.cpp
  // compile with: /EHsc
  #include <string>
  #include <iostream>
  using namespace std;
  int main( )
  {
  using namespace std;
  string str1 ("Heterological paradoxes are persistent.");
  cout << "The original string str1 is: \n " << str1
  << endl << endl;
  basic_string <char> str2 = str1.substr ( 6 , 7 );
  cout << "The substring str1 copied is: " << str2
  << endl << endl;
  basic_string <char> str3 = str1.substr ( );
  cout << "The default substring str3 is: \n " << str3
  << "\n which is the entire original string." << endl;
  输出结果:
  The original string str1 is:
  Heterological paradoxes are persistent.
  The substring str1 copied is: logical
  The default substring str3 is:
  Heterological paradoxes are persistent.
  which is the entire original string.
  }
  在oracle中的用法:
  SUBSTR(:NEW.FLAGSTATUS,17,1)
  其中参数依次是 ( 串,开始,长度),并返回子串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值