string Type

Notes from C++ Primer

Operations

Operations of string support lots of operations of sequential container.

  • string s;          define a new empty string object, named s.
  • string s(cp);    define a new string object, initialized by a C-style string pointed by cp.
  • string s(s2);    define a new string object initialized by the copy of s2.
  • is >> s;          read a string splited by space from input stream is, write into s.
  • os << s;         output the s into output stream os.
  • getline(is, s)    get a line of string from input stream is, output into s.
  • s1 + s2           concatenate string s1 and string s2.
  • s1 += s2        concatenate string s2 at the end of string s1.

As the operation of string is almost the same as container, many executions of vector can be replaced like codes below:

string s("Hiya!");
string::iterator iter = s.begin();
while(iter != s.end())
	cout << *iter++ << endl;	// postfix increment: print old value

Operations only for string

There‘re three base operations supported by string type but container type.

  • substr function, return the sub-string of current string object.
  • append and replace function, modify string object.
  • a series of find function, which is used to find string object.

Operations of sub-string:

  • s.substr(pos, n)    return a substring of s, from position pos with length n in s.
  • s.substr(pos)        return a substring of s, from position pos to the end of s.
  • s.substr()             return a copy of s.

1. substr

We can pass the beginning position pos of ing substring and a counter n which determines the length of substring to function substr to finish returning substring.

string s("hello world");

// return substring of 5 characters starting at position 6
string s2 = s.substr(6, 5);		// s2 = world

An alternative way is:

// return substring from position 6 to the end of s
string s3 = s.substr(6);		// s3 = world

2. append and replace

append function offers an shortcut to insert string at the end of string:

string s("C++ Primer");		// initialize s to "C++ Primer"
s.append(" 3rd Ed.");		// s == "C++ Primer 3rd Ed."

// equivalent to s.append(" 3rd Ed.")
s.insert(s.size(), " 3rd ED.");

replace function is a shortcut of deleting some characters and then inserting other contents:

// starting at position 11, erase 3 characters and then insert "4th"
s.replace(11, 3, "4th");	// s == "C++ Primer 4th Ed."

// equivalent way to replace "3rd" by "4th"
s.erase(11, 3);				// s == "C++ Primer Ed."
s.insert(11, "4th");		// s == "C++ Primer 4th Ed."

Also, we don‘t need to require the length of inserting string is the same as the length of deleting string. Thus we can replace longer or shorter string:

s.replace(11, 3, "Fourth");		// s == "C++ Primer Fourth Ed."

3. find operations of string

string class offers 6 kinds of serarch function. They all return a string::size_type type value indicating the position of match, or return a special value string::npos indicating fail. string class define npos as a value larger than any validate index of string.

  • s.find(args)                      find the first position match with args in s.
  • s.rfind(args)                     find the last position match with args in s.
  • s.find_first_of(args)          find the first position match with any character in args in s.
  • s.find_last_of(args)           find the last position match with any character in args in s.
  • s.find_first_not_of(args)    find the first position of character not belong to args in s.
  • s.find_first_not_of(args)    find the first position of character not belong to args in s.

The simple accurate search is find function.

string name("AnnaBelle");
string::size_type pos1 = name.find("Anna");		// pos1 == 0

By default, find operation is case sensitive:

string lowercase("annabelle");
pos1 = lowercase.find("Anna");		// pos1 == npos
时间: 2024-10-14 06:59:03

string Type的相关文章

【C++注意事项】6 Library string Type

Processing Every Character? Use Range-Based for If we want to do something to every character in a string, by far the best approach is to use a statement introduced by the new standard: the range for statement. This statement iterates through the ele

Excel Sheet Column Title (STRING - TYPE CONVERTION)

QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB FIRST TRY class Solution { public: string convertToTitle(int n) { int r

[C++] String Basic

Namespace Declarations A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix. // using namespace::name using std::vector using std::string  A sperate using declaration is required for each name

JAVA中int、String的类型转换

int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i);这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? String -> int s="12345";int i;第一种方法:i=Integer.parseInt(s);第二种方法:i=Integer.valueOf(s).intValue();这两种方法有什么区别

C++ supports various string and character types,

String and Character Literals (C++) Visual Studio 2015 Other Versions C++ supports various string and character types, and provides ways to express literal values of each of these types. In your source code, you express the content of your character

java中Date与String的相互转化

1:大体思路 [html] view plaincopy 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04-14"); 方法2: SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 String dstr="2008-4-24"; j

java中字符串String 转 int(转)

java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue(); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=

解决Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

在项目中添加src中添加NativeIO类 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF lice

30天C#基础巩固------了解委托,string练习

---->了解委托.     生活中的例子:我要打官司,我需要找一个律师,法庭上面律师为当事人辩护,它真正执行的是当事人的陈词,这时律师 就相当于一个委托对象.当事人则委托律师为自己辩解.     C#中的委托是一个类,(类类型),因为只有类才有对象的概念.C#中的委托可以理解为函数的一个包装,它使得C#中的函数可以作为参数来传递. 委托的定义: //定义委托 public delegate void MyDelegate(int para1,string para2); 委托的一些步奏: