當前位置:首頁 » 編程語言 » c語言中string函數解釋
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言中string函數解釋

發布時間: 2022-08-08 14:58:46

1. c++中c_str()用法,越詳細越好。

應該這樣用:

char c[20];

string s="1234";

strcpy(c,s.c_str());

這樣才不會出錯,c_str()返回的是一個臨時指針,不能對其進行操作。

語法: const char *c_str();

c_str()函數返回一個指向正規C字元串的指針, 內容與本string串相同.,這是為了與c語言兼容,在c語言中沒有string類型,故必須通過string類對象的成員函數c_str()把string 對象轉換成c中的字元串樣式。

注意:一定要使用strcpy()函數 等來操作方法c_str()返回的指針。

(1)c語言中string函數解釋擴展閱讀:

關於上述用法種最好不要這樣用

constchar*c;

std::strings="1234";

c=s.c_str();

std::cout<<c<<std::endl;//輸出:1234

s="abcd";

std::cout<<c<<std::endl;//輸出:abcd

上面如果繼續用c指針的話,導致的錯誤將是不可想像的,就如:1234變為abcd。

2. c語言中string函數和conio函數分別是什麼意思怎麼個用法

兩個函數庫,string庫是C語言的標准庫,conio是WINDOWS系統提供的控制台函數庫.

stirng庫提供了諸如求字元串長度,連接字元串,復制字元串等字元串操作函數.

conio提示了字元讀取(如getch()),游標控制(如gotoxy()).窗口控制都系統函數.

3. C語言中有string嗎

C語言中沒有string類型。

string類型是 C++、java、VB等編程語言中的。 在java、C#中,String類是不可變的,對String類的任何改變,都是返回一個新的String類對象。string 是C++標准程序庫中的一個頭文件,定義了C++標准中的字元串的基本模板類std::basic_string及相關的模板類實例。

而在C語言中,C語言中沒有字元串類型,字元串是存放在字元型數組中的,將字元串作為字元數組來處理的。為了測定字元串的實際長度,C語言規定了一個「字元串結束標志」,以字元''作為結束標志 。

(3)c語言中string函數解釋擴展閱讀:

C++中string的使用:

其中的string是以char作為模板參數的模板類實例,把字元串的內存管理責任由string負責而不是由編程者負責,大大減輕了C語言風格的字元串的麻煩。std::basic_string提供了大量的字元串操作函數,如比較、連接、搜索、替換、獲得子串等。並可與C語言風格字元串雙向轉換。

字元串在C語言中的使用:

字元串常量初始化數組。如:char c[ ]={『c』,『 』,『p』,『r』,『o','g','r','a','m'};可寫為:char c[ ]={"C program"}; 或去掉{}寫為:char c[ ]="C program"。

4. c語言中str是什麼意思

str -- 通常,程序員喜歡用它作字元串變數名。它是 string(英文詞、字元串的縮寫)

Str函數,返回代表一數值的 Variant(String)。語法Str(number)必要的 number 參數為一 Long,其中可包含任何有效的數值表達式。說明當一數字轉成字元串時,總會在前頭保留一空位來表示正負。



(4)c語言中string函數解釋擴展閱讀:

如果 number 為正,返回的字元串包含一前導空格暗示有一正號。使用 Format 函數可將數值轉成必要的格式,如日期、時間、貨幣或其他用戶自定義格式。與 Str 不同的是,Format 函數不包含前導空格來放置 number 的正負號。

注意 Str 函數只視句點 (.) 為有效的小數點。如果使用不同的小數點(例如,國際性的應用程序),可使用 CStr 將數字轉成字元串。

5. c語言中string怎麼用啊

用於輸入輸出的字元串函數, 在使用前應包含頭文件"stdio.h" ; 使用其它字元串函數則應包含頭文件"string.h"。

6. C語言中string指令是什麼

string 是 c++的,不是 c 的
stirng 是C++裡面一個用來處理字元串的類。包含了字元串處理掉一下常用方法,如:

Constructors 構造函數,用於字元串初始化
Operators 操作符,用於字元串比較和賦值
append() 在字元串的末尾添加文本
assign() 為字元串賦新值
at() 按給定索引值返回字元
begin() 返回一個迭代器,指向第一個字元
c_str() 將字元串以C字元數組的形式返回
capacity() 返回重新分配空間前的字元容量
compare() 比較兩個字元串
() 將內容復制為一個字元數組
data() 返回內容的字元數組形式
empty() 如果字元串為空,返回真
end() 返回一個迭代器,指向字元串的末尾。(最後一個字元的下一個位置)
erase() 刪除字元
find() 在字元串中查找字元
find_first_of() 查找第一個與value中的某值相等的字元
find_first_not_of() 查找第一個與value中的所有值都不相等的字元
find_last_of() 查找最後一個與value中的某值相等的字元
find_last_not_of() 查找最後一個與value中的所有值都不相等的字元
get_allocator() 返回配置器
insert() 插入字元
length() 返回字元串的長度
max_size() 返回字元的最大可能個數
rbegin() 返回一個逆向迭代器,指向最後一個字元
rend() 返回一個逆向迭代器,指向第一個元素的前一個位置
replace() 替換字元
reserve() 保留一定容量以容納字元串(設置capacity值)
resize() 重新設置字元串的大小
rfind() 查找最後一個與value相等的字元(逆向查找)
size() 返回字元串中字元的數量
substr() 返回某個子字元串
swap() 交換兩個字元串的內容

7. c語言中string是啥意思啊 控制啥的啊

編程語言中的字元串,用雙引號引起來的幾個字元.如"Abc","一天"。String類是不可變(final)的,對String類的任何改變,都是返回一個新的String類對象。

這樣的話把String類的引用傳遞給一個方法,該方法對String的任何改變,對原引用指向的對象沒有任何影響,這一點和基本數據類型相似。

string s1,s2;

s1="abc";

s2=s1;

s2="def";

1、用法

string類的構造函數:

string(const char*s);//用c字元串s初始化string(int n,char c);//用n個字元c初始化

此外,string類還支持默認構造函數和復制構造函數,如string s1;string s2="hello";都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常。

2、string類的字元操作:

const char&operator[](int n)const;const char&at(int n)const;char&operator[](int n);char&at(int n);

operator[]和at()均返回當前字元串中第n個字元的位置,但at函數提供范圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問

const char*data()const;//返回一個非null終止的c字元數組const char*c_str()const;//返回一個以null終止的c字元串

int (char*s,int n,int pos=0)const;//把當前串中以pos開始的n個字元拷貝到以s為起始位置的字元數組中,返回實際拷貝的數目。

(7)c語言中string函數解釋擴展閱讀:

1、string的特性描述:

int capacity()const; //返回當前容量(即string中不必增加內存即可存放的元素個數)

int max_size()const; //返回string對象中可存放的最大字元串的長度

int size()const; //返回當前字元串的大小

int length()const; //返回當前字元串的長度

bool empty()const; //當前字元串是否為空

void resize(int len,char c);//把字元串當前大小置為len,並用字元c填充不足的部分

2、string類的輸入輸出操作:

string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作。函數getline(istream &in,string &s);用於從輸入流in中讀取字元串到s中,以換行符' '分開。

3、string的賦值:

string &operator=(const string &s);//把字元串s賦給當前字元串

string &assign(const char *s);//用c類型字元串s賦值

string &assign(const char *s,int n);//用c字元串s開始的n個字元賦值

string &assign(const string &s);//把字元串s賦給當前字元串

string &assign(int n,char c);//用n個字元c賦值給當前字元串

string &assign(const string &s,int start,int n);//把字元串s中從start開始的n個字元賦給當前字元

string &assign(const_iterator first,const_itertor last); //把first和last迭代器之間的部分賦給字元串

4、string的連接:

string &operator+=(const string &s);//把字元串s連接到當前字元串的結尾

string &append(const char *s); //把c類型字元串s連接到當前字元串結尾

string &append(const char *s,int n);//把c類型字元串s的前n個字元連接到當前字元串結尾

string &append(const string &s); //同operator+=()

string &append(const string &s,int pos,int n);//把字元串s中從pos開始的n個字元連接到當前字元串的結尾

string &append(int n,char c); //在當前字元串結尾添加n個字元c

string &append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當前字元串的結尾

5、string的子串:

string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字元組成的字元串

6、string的交換:

void swap(string &s2); //交換當前字元串與s2的值

7、string類的查找函數:

int find(char c, int pos = 0) const;//從pos開始查找字元c在當前字元串的位置

int find(const char *s,int pos = 0) const;//從pos開始查找字元串s在當前串中的位置

int find(const char *s, int pos, int n) const;//從pos開始查找字元串s中前n個字元在當前串中的位置

int find(const string &s,int pos = 0) const;//從pos開始查找字元串s在當前串中的位置//查找成功時返回所在位置,失敗返回string::npos的值

int rfind(char c, int pos = npos) const;//從pos開始從後向前查找字元c在當前串中的位置

int rfind(const char *s, int pos = npos) const;

int rfind(const char *s, int pos, int n = npos) const;

int rfind(const string &s,int pos = npos) const;//從pos開始從後向前查找字元串s中前n個字元組成的字元串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值

int find_first_of(char c, int pos = 0) const;//從pos開始查找字元c第一次出現的位置

int find_first_of(const char *s, int pos = 0) const;

int find_first_of(const char *s, int pos, int n) const;

int find_first_of(const string &s,int pos = 0) const;//從pos開始查找當前串中第一個在s的前n個字元組成的數組里的字元的位置。查找失敗返回string::npos

int find_first_not_of(char c, int pos = 0) const;

int find_first_not_of(const char *s, int pos = 0) const;

int find_first_not_of(const char *s, int pos,int n) const;

int find_first_not_of(const string &s,int pos = 0) const;//從當前串中查找第一個不在串s中的字元出現的位置,失敗返回string::npos

int find_last_of(char c, int pos = npos) const;

int find_last_of(const char *s, int pos = npos) const;

int find_last_of(const char *s, int pos, int n = npos) const;

int find_last_of(const string &s,int pos = npos) const;

int find_last_not_of(char c, int pos = npos) const;

int find_last_not_of(const char *s, int pos = npos) const;

int find_last_not_of(const char *s, int pos, int n) const;

int find_last_not_of(const string &s,int pos = npos) const;//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找。

string類的替換函數:

string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個字元,然後在p0處插入字元串s的前n個字元

string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個字元,然後在p0處插入串s中從pos開始的n個字元

string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個字元,然後在p0處插入n個字元c

string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換為字元串s

string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換為s的前n個字元。

string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換為串s

string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換為n個字元c

string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字元串。

string類的插入函數:

string &insert(int p0, const char *s);

string &insert(int p0, const char *s, int n);

string &insert(int p0,const string &s);

string &insert(int p0,const string &s, int pos, int n);//前4個函數在p0位置插入字元串s中pos開始的前n個字元

string &insert(int p0, int n, char c);//此函數在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字元

void insert(iterator it, int n, char c);//在it處插入n個字元c

string類的刪除函數

iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置。

iterator erase(iterator it);//刪除it指向的字元,返回刪除後迭代器的位置。

string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字元,返回修改後的字元串。

string類的迭代器處理:

string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字元的語法,類似於指針操作,迭代器不檢查范圍。

用string::iterator或string::const_iterator聲明迭代器變數,const_iterator不允許改變迭代的內容。常用迭代器函數有:

const_iterator begin()const;iterator begin(); //返回string的起始位置

const_iterator end()const;iterator end(); //返回string的最後一個字元後面的位置

const_iterator rbegin()const;iterator rbegin(); //返回string的最後一個字元的位置

const_iterator rend()const;iterator rend(); //返回string第一個字元位置的前面

rbegin和rend用於從後向前的迭代訪問,通過設置迭代器string::reverse_iterator或string::const_reverse_iterator實現

字元串流處理:

通過定義ostringstream和istringstream變數實現,在#include <sstream>頭文件中。

例如:

string input("hello,this is a test");

istringstream is(input);

string s1,s2,s3,s4;

is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"

ostringstream os;

os<<s1<<s2<<s3<<s4;

cout<<os.str();

8. string在c語言中是什麼意思

String是C++、java、VB等編程語言中的字元串,用雙引號引起來的幾個字元,如"Abc","一天"。

在java、C#中,String類是不可變的,對String類的任何改變,都是返回一個新的String類對象。 String 對象是System.Char對象的有序集合,用於表示字元串。String 對象的值是該有序集合的內容,並且該值是不可變的。

(8)c語言中string函數解釋擴展閱讀:

string字元創的用法:

1、字元串輸出函數puts,格式:puts (字元數組名),功能:把字元數組中的字元串輸出到顯示器。

2、字元串輸入函數gets,格式:gets (字元數組名),功能:從標准輸入設備鍵盤上輸入一個字元串。 本函數得到一個函數值,即為該字元數組的首地址。

3、字元串連接函數strcat,格式:strcat (字元數組名1,字元數組名2),功能:把字元數組2中的字元串連接到字元數組1中字元串的後面,並刪去字元串1後的串標志「」。本函數返回值是字元數組1的首地址。

參考資料來源:網路-string

9. c語言中,string是做什麼用的,,怎麼用,請詳細說下

string是一個頭文件
#include<string.h>
就包涵了對字元處理的一些函數,
比如strlen用來計算串長度
strcpy是串拷貝函數
strcmp是串比較函數
。。。。。。。
必須包涵string.h這個頭文件,才能調用這些函數

10. c語言中strlen、str、len分別指的是什麼意思

strlen -- 計算字元串長度的庫函數名。
str -- 通常,程序員喜歡用它作 字元串 變數名。它是 string(英文詞 字元串 的縮寫)
len -- 通常,程序員喜歡用它作 變數名。它是 length(英文詞 長度 的縮寫)。
例如:
char str[20]="I am a student";
int len;
len = strlen(str);
printf("the string length is: %d",len);