導航:首頁 > 使用方法 > string使用方法

string使用方法

發布時間:2022-12-19 09:15:56

『壹』 c++語言中string怎麼

1.
定義和構造初始化string 提供了很多構造函數,可以以多種方式來初始化string字元串

2.
賦值,拼接字元串string重載了 = + += 等多種運算符,讓字元串組合拼接更簡單

3.
訪問字元操作string可以按數組方式,以下標來訪問。還可以用at()函數訪問指定的字元

4.
可以使用 STL 的介面可以把 string 理解為一個特殊的容器,容器中裝的是字元

5.
比較操作 == != > >= < <= compare 等string的比較操作,按字元在字典中的順序進行逐一比較

『貳』 String常使用的方法有哪些

1char charAt (int index) 返回index所指定的字元

2String concat(String str) 將兩字元串連接

3boolean endsWith(String str) 測試字元串是否以str結尾

4boolean equals(Object obj) 比較兩對象

5char[] getBytes 將字元串轉換成字元數組返回

6 char[] getBytes(String str) 將指定的字元串轉成制服數組返回

7boolean startsWith(String str) 測試字元串是否以str開始

8 int length() 返回字元串的長度

9 String replace(char old ,char new) 將old用new替代

10 char[] toCharArray 將字元串轉換成字元數組

11 String toLowerCase() 將字元串內的字元改寫成小寫

12String toUpperCase() 將字元串內的字元改寫成大寫

13String valueOf(Boolean b) 將布爾方法b的內容用字元串表示

14String valueOf(char ch) 將字元ch的內容用字元串表示

15String valueOf(int index) 將數字index的內容用字元串表示

16 String valueOf(long l) 將長整數字l的內容用字元串表示

17String substring(int1,int2) 取出字元串內第int1位置到int2的字元串

『叄』 c語言中string怎麼用啊

C語言提供了豐富的字元串處理函數, 大致可分為字元串的輸入、輸出、合並、修改、比較、轉換、復制、搜索幾類。 使用這些函數可大大減輕編程的負擔。用於輸入輸出的字元串函數, 在使用前應包含頭文件"stdio.h" ; 使用其它字元串函數則應包含頭文件"string.h"。 下面介紹幾個最常用的字元串函數。
1.字元串輸出函數 puts 格式: puts (字元數組名) 功能:把字元數組中的字元串輸出到顯示器。 即在屏幕上顯示該字元串
#include"stdio.h"
main()
{
static char c[]="BASIC\ndBASE";
puts(c);
}
2.字元串輸入函數gets 格式: gets (字元數組名) 功能:從標准輸入設備鍵盤上輸入一個字元串。 本函數得到一個函數值,即為該字元數組的首地址。
#include"stdio.h"
main()
{
char st[15];
printf("input string:\n");
gets(st);
puts(st);
}
3.字元串連接函數strcat 格式: strcat (字元數組名1,字元數組名2) 功能:把字元數組2中的字元串連接到字元數組1 中字元串的後面,並刪去字元串1後的串標志「\0」。本函數返回值是字元數組1的首地址。
#include"string.h"
main()
{
static char st1[30]="My name is ";
int st2[10];
printf("input your name:\n");
gets(st2);
strcat(st1,st2);
puts(st1);
}
4.字元串拷貝函數strcpy 格式: strcpy (字元數組名1,字元數組名2) 功能:把字元數組2中的字元串拷貝到字元數組1中。串結束標志「\0」也一同拷貝。字元數名2, 也可以是一個字元串常量。這時相當於把一個字元串賦予一個字元數組。
#include"string.h"
main()
{
static char st1[15],st2[]="C Language";
strcpy(st1,st2);
puts(st1);printf("\n");
}
5.字元串比較函數strcmp 格式: strcmp(字元數組名1,字元數組名2) 功能:按照ASCII碼順序比較兩個數組中的字元串,並由函數返回值返回比較結果。
字元串1=字元串2,返回值=0;
字元串2〉字元串2,返回值〉0;
字元串1〈字元串2,返回值〈0。
本函數也可用於比較兩個字元串常量,或比較數組和字元串常量。
#include"string.h"
main()
{ int k;
static char st1[15],st2[]="C Language";
printf("input a string:\n");
gets(st1);
k=strcmp(st1,st2);
if(k==0) printf("st1=st2\n");
if(k>0) printf("st1>st2\n");
if(k<0) printf("st1<st2\n");
}
6.測字元串長度函數strlen 格式: strlen(字元數組名) 功能:測字元串的實際長度(不含字元串結束標志『\0』) 並作為函數返回值。
#include"string.h"
main()
{ int k;
static char st[]="C language";
k=strlen(st);
printf("The lenth of the string is %d\n",k);
}

『肆』 String常使用的方法有哪些

這些是最常用的:
char charAt (int index) 返回index所指定的字元
String concat(String str) 將兩字元串連接
boolean endsWith(String str) 測試字元串是否以str結尾
boolean equals(Object obj) 比較兩對象
char[] getBytes 將字元串轉換成字元數組返回
char[] getBytes(String str) 將指定的字元串轉成制服數組返回
boolean startsWith(String str) 測試字元串是否以str開始
int length() 返回字元串的長度
String replace(char old ,char new) 將old用new替代
char[] toCharArray 將字元串轉換成字元數組
String toLowerCase() 將字元串內的字元改寫成小寫
String toUpperCase() 將字元串內的字元改寫成大寫
String valueOf(Boolean b) 將布爾方法b的內容用字元串表示
String valueOf(char ch) 將字元ch的內容用字元串表示
String valueOf(int index) 將數字index的內容用字元串表示
String valueOf(long l) 將長整數字l的內容用字元串表示
String substring(int1,int2) 取出字元串內第int1位置到int2的字元串

=============
以下解釋的十分清楚了,還有例子
1、length() 字元串的長度
例:char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();
2、charAt() 截取一個字元
例:char ch;
ch="abc".charAt(1); 返回'b'
3、 getChars() 截取多個字元
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart指定了子串開始字元的下標,sourceEnd指定了子串結束後的下一個字元的下標。因此, 子串包含從sourceStart到sourceEnd-1的字元。接收字元的數組由target指定,target中開始復制子串的下標值是targetStart。
例:String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);
4、getBytes()
替代getChars()的一種方法是將字元存儲在位元組數組中,該方法即getBytes()。

5、toCharArray()
6、equals()和equalsIgnoreCase() 比較兩個字元串
7、regionMatches() 用於比較一個字元串中特定區域與另一特定區域,它有一個重載的形式允許在比較中忽略大小寫。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8、startsWith()和endsWith()
startsWith()方法決定是否以特定字元串開始,endWith()方法決定是否以特定字元串結束
9、equals()和==
equals()方法比較字元串對象中的字元,==運算符比較兩個對象是否引用同一實例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比較字元串
11、indexOf()和lastIndexOf()
indexOf() 查找字元或者子串第一次出現的地方。
lastIndexOf() 查找字元或者子串是後一次出現的地方。
12、substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)
13、concat() 連接兩個字元串
14 、replace() 替換
它有兩種形式,第一種形式用一個字元在調用字元串中所有出現某個字元的地方進行替換,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');
第二種形式是用一個字元序列替換另一個字元序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
15、trim() 去掉起始和結尾的空格
16、valueOf() 轉換為字元串
17、toLowerCase() 轉換為小寫
18、toUpperCase() 轉換為大寫
19、StringBuffer構造函數
StringBuffer定義了三個構造函數:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。
(2)、ensureCapacity() 設置緩沖區的大小
void ensureCapacity(int capacity)
(3)、setLength() 設置緩沖區的長度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、append() 可把任何類型數據的字元串表示連接到調用的StringBuffer對象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();
(7)、insert() 插入字元串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定將字元串插入到StringBuffer對象中的位置的下標。
(8)、reverse() 顛倒StringBuffer對象中的字元
StringBuffer reverse()
(9)、delete()和deleteCharAt() 刪除字元
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替換
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)

『伍』 String類的幾個常用方法

一. Java的api-docs文檔組成
1. 在docs中,Java中任何一個類的文檔由如下幾部分組成:
    ★類的相關定義,包括類的名稱,有哪些父類,有哪些介面;
    ★類的相關簡介,包括一些基本的使用說明;
    ★成員(Field)摘要:屬性就是一種成員,會列出所有出現的成員信息項;
    ★構造方法(Constructor)說明:列出該類中所有構造方法的信息;
    ★方法信息(Method)說明:所有類中定義好的可以使用的方法;
    ★成員、構造、方法的詳細信息。
二. 字元串與字元數組
1. 字元串就是一個字元數組,所以在String類裡面支持有字元數組轉換為字元串以及字元串變為字元的處理操作方法。這些處理操作方法如下:
ToDo
char ch = 'a' ;
//ch = (char) (ch - 32) ;
ch -= 32 ;   // 這樣簡寫可以避免像上面一樣寫強制轉換並且避免出現異常
三. 位元組與字元串
1. 位元組更多的情況是用於數據傳輸以及編碼轉換處理之中,在String類裡面提供有對位元組操作的支持。
2. 位元組並不適合處理中文,而只有字元適合於處理中文,並且按照程序的概念來講,一個字元等於2個位元組,位元組只適合於處理二進制數據。

『陸』 c++ string 怎麼用

用法和步驟:

1.定義和構造初始化

string 提供了很多構造函數,可以以多種方式來初始化string字元串。




注意事項:

1.使用string,必須要包含頭文件string.h

2.C++中,最好使用string來代替char[ ]

『柒』 java中string怎麼使用

當執行string
a="abc";時,java虛擬機會在棧中創建三個char型的值'a'、'b'和'c',然後在堆中創建一個string對象,它的值(value)是剛才在棧中創建的三個char型值組成的數組{'a','b','c'},最後這個新創建的string對象會被添加到字元串池中。如果我們接著執行string
b=new
string("abc");代碼,由於"abc"已經被創建並保存於字元串池中,因此java虛擬機只會在堆中新創建一個string對象,但是它的值(value)是共享前一行代碼執行時在棧中創建的三個char型值值'a'、'b'和'c'.
string
a="abc";
創建了一個對象
這個對象是在字元串池裡吧
而不是堆里
所以只創建了一個對象
而string
b=new
string("abc");在字元串常量池以及
堆里都有對象所以是兩個對象
string
a="abc";
在常量池中
new
string("abc");
在堆中
string
str1
=
"abc";引用的對象在棧(或者叫string池)中。
string
str1
=new
string
("abc");
引用的對象在內存/堆中。

『捌』 java中string怎麼使用

以下是關於string的七種用法,注意哦,記得要時常去查看java的API文檔,那個裡面也有很詳細的介紹

1>獲取

1.1:字元串中包含的字元數,也就是字元串的長度。
int length():獲取長度

1.2:根據位置獲取位置上某個字元。
char charAt(int index)

1.3:根據字元獲取該字元在字元串中的位置。
int indexOf(int ch):返回的是ch在字元串中第一次出現的位置。
int indexOf(int ch,int fromIndex):從fromIndex指定位置開始,獲取ch在字元串中出現的位置。

int indexOf(String str):返回的是str在字元串中第一次出現的位置。
int indexOf(String str,int fromIndex):從fromIndex指定位置開始,獲取str在字元串中出現的位置。

1.4:int lastIndexOf(String str):反向索引。

2>判斷
2.1:字元串中是否包含某一個子串。
boolean contains(str);
特殊之處:indexOf(str):可以索引str第一次出現為止,如果返回-1,表示該str不在字元串中存在。
所以,也可以用於對指定判斷是否包含。
if(str.indexOf("a")!=1)
而且該方法既可以判斷,也可以獲取出現的位置。
2.2:字元串中是否有內容。
boolean isEmpty():原理就是判斷長度是否為0。

2.3:字元串是否以指定內容開頭。
boolean startsWith(str);

2.4:字元串是否以指定內容結尾。
boolean endsWith(str);

2.5:判斷字元內容是否相同,復寫了object類中的equals方法。
boolean equals(str);

2.6:判斷內容是否相同,並忽略大小寫。
boolean.equalsIgnorecase();
3>轉換

3.1:將字元數組轉成字元串。
構造函數:String(char[])
String(char[],offset,count):將字元數組中的一部分轉成字元串
靜態方法:
static String ValueOf(char[]);
static String ValueOf(char[] data,int offset,int count);
static String valueOf(char[]);

3.2:將字元串轉成字元組
char[] tocharArray();
3.3:將位元組數組轉成字元串。
String(byte[])
String(byte[],offset,count):將位元組數組中的一部分轉成字元串

3.4:將字元串轉成位元組數組。
byte[] getBytes()
3.5:將基本數據類型轉成字元串,
static String valueOf(int)
static String valueOf(double)
// 3+"" 與 String.valueOf(3)的值是一樣的
特殊:字元串和位元組數組在轉換過程中,是可以指定編碼的。
4>替換
String replace(oldchar,newchar);
5>切割
String[] split(regex);
6>子串。獲取字元串中的而一部分
String subString(begin);
String subString(begin,end);
7>轉換,去除空格,比較。

7.1:將字元串轉成大寫或小寫
String toUpperCsae() 大轉小
String toLowerCsae() 小轉大
7.2:將字元串兩端的多個空格去除
String trim();
7.3:對兩個字元串進行自然順序的比較
int compareTo(string);
請看如下代碼,下面的代碼都是針對上面string七種用法而進行一一舉例說明:

復制代碼 代碼如下:

class StringMethodDemo
{
public static void method_Zhuanhuan_Qukong_Bijiao()
{
String s = " hello Java ";

//列印結果是:(hello和java前後門都有空格)hello java
sop(s.toUpperCase());

//列印結果是:(HELLO和JAVA前後門都有空格)HELLO JAVA
sop(s.toLowerCase());
//列印及結果是:不帶空格的「hello java」
sop(s.trim());
//比較數的大寫,列印結果是:1,因為b對應ascii值是98,
//a對應是97,所以b-a=1
String s1 = "abc";
String s2 = "aaa";
sop(s1.compareTo(s2));
}
public static void method_sub()
{
String s = "abcdef";
//列印結果是:cdef,從指定位置開始到結尾。如果角標不存在,會出現字元串角標越界。
sop(s.substring(2));
//列印結果是:cd,包含頭,不包含尾。
sop(s.substring(2,4));
}
public static void method_split()
{
String s = "zhangsan,lisi,wangwu";
String[] arr = s.split(",");
for(int x=0; x<arr.length; x++)
{
sop(arr[x]);
}
}
public static void method_replace()
{
String s = "hello java";
//String s1 = s.replace('a','n');
//String s1 = s.replace('w','n'); 如果要替換的字元不存在,返回的還是原串

String s1 = s.replace("java","world");//列印結果是:hello world
sop("s="+s); //列印結果是:hello java因為字元串一旦被初始化,值就不可被改變
sop("s1="+s1);//列印結果是:hello jnvn
}
public static void method_trans()
{
char[] arr = {'a','b','c','d','e','f'};
String s = new String(arr,1,3);
sop("s="+s);//列印結果是:bcd
String s1 = "zxcvbnm";
char[] chs = s1.toCharArray();
for(int x=0; x<chs.length; x++)
{
sop("ch="+chs[x]);//列印結果是:ch=z,x,c,v,b,n,m
}
}
public static void method_is()
{
String str = "ArrayDemo.java";
//判斷文件名稱是否是Array單詞開頭
sop(str.startsWith("Array"));

//判斷文件名稱是否是.java的文件
sop(str.endsWith(".java"));

//判斷文件中是否包含Demo
sop(str.contains("Demo"));
}

public static void method_get()
{
String str = "abcdeakpf";
//長度
sop(str.length());
//根據索引獲取字元
sop(str.charAt(4));
//sop(str.charAt(40));當訪問到字元串中不存在的角標時會發生(字元串角標越界異常)
//根據字元獲取索引
//sop(str.indexOf('a'));
sop(str.indexOf('a',3));//列印的是5,因為角標3是d,
//所以從d後面開始找a,第5個角標是a
//sop(str.indexOf('t',3))列印:-1,如果沒有找到角標,返回-1

//反向索引一個字元出現的位置(從右往左查找,但是角標還是從左開始)
sop(str.lastIndexOf("a"));
}
public static void main(String[] args)
{
method_Zhuanhuan_Qukong_Bijiao();
//method_sub();
//method_split();
//method_replace();
//method_trans();
//method_is();
//method_get();
/*
String s1 = "abc";
String s2 = new String("abc");
String s3 = "abc";
System.out.println(s1==s2);
System.out.println(s1==s3);
*/
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}

『玖』 java中string類的方法有哪些

方法摘要
char charAt(int index)
返回指定索引處的 char 值。
int codePointAt(int index)
返回指定索引處的字元(Unicode 代碼點)。
int codePointBefore(int index)
返回指定索引之前的字元(Unicode 代碼點)。
int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范圍中的 Unicode 代碼點數。
int compareTo(String anotherString)
按字典順序比較兩個字元串。
int compareToIgnoreCase(String str)
不考慮大小寫,按字典順序比較兩個字元串。
String concat(String str)
將指定字元串聯到此字元串的結尾。
boolean contains(CharSequence s)
當且僅當此字元串包含 char 值的指定序列時,才返回 true。
boolean contentEquals(CharSequence cs)
當且僅當此 String 表示與指定序列相同的 char 值時,才返回 true。
boolean contentEquals(StringBuffer sb)
當且僅當此 String 表示與指定的 StringBuffer 相同的字元序列時,才返回 true。
static String ValueOf(char[] data)
返回指定數組中表示該字元序列的字元串。
static String ValueOf(char[] data, int offset, int count)
返回指定數組中表示該字元序列的字元串。
boolean endsWith(String suffix)
測試此字元串是否以指定的後綴結束。
boolean equals(Object anObject)
比較此字元串與指定的對象。
boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 進行比較,不考慮大小寫。
static String format(Locale l, String format, Object... args)
使用指定的語言環境、格式字元串和參數返回一個格式化字元串。
static String format(String format, Object... args)
使用指定的格式字元串和參數返回一個格式化字元串。
byte[] getBytes()
使用平台默認的字元集將此 String 解碼為位元組序列,並將結果存儲到一個新的位元組數組中。
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已過時。 該方法無法將字元正確轉換為位元組。從 JDK 1.1 起,完成該轉換的首選方法是通過 getBytes() 構造方法,該方法使用平台的默認字元集。
byte[] getBytes(String charsetName)
使用指定的字元集將此 String 解碼為位元組序列,並將結果存儲到一個新的位元組數組中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字元從此字元串復制到目標字元數組。
int hashCode()
返回此字元串的哈希碼。
int indexOf(int ch)
返回指定字元在此字元串中第一次出現處的索引。
int indexOf(int ch, int fromIndex)
從指定的索引開始搜索,返回在此字元串中第一次出現指定字元處的索引。
int indexOf(String str)
返回第一次出現的指定子字元串在此字元串中的索引。
int indexOf(String str, int fromIndex)
從指定的索引處開始,返回第一次出現的指定子字元串在此字元串中的索引。
String intern()
返回字元串對象的規范化表示形式。
int lastIndexOf(int ch)
返回最後一次出現的指定字元在此字元串中的索引。
int lastIndexOf(int ch, int fromIndex)
從指定的索引處開始進行後向搜索,返回最後一次出現的指定字元在此字元串中的索引。
int lastIndexOf(String str)
返回在此字元串中最右邊出現的指定子字元串的索引。
int lastIndexOf(String str, int fromIndex)
從指定的索引處開始向後搜索,返回在此字元串中最後一次出現的指定子字元串的索引。
int length()
返回此字元串的長度。
boolean matches(String regex)
通知此字元串是否匹配給定的正則表達式。
int offsetByCodePoints(int index, int codePointOffset)
返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字元串區域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字元串區域是否相等。
String replace(char oldChar, char newChar)
返回一個新的字元串,它是通過用 newChar 替換此字元串中出現的所有 oldChar 而生成的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替換序列替換此字元串匹配字面值目標序列的每個子字元串。
String replaceAll(String regex, String replacement)
使用給定的 replacement 字元串替換此字元串匹配給定的正則表達式的每個子字元串。
String replaceFirst(String regex, String replacement)
使用給定的 replacement 字元串替換此字元串匹配給定的正則表達式的第一個子字元串。
String[] split(String regex)
根據給定的正則表達式的匹配來拆分此字元串。
String[] split(String regex, int limit)
根據匹配給定的正則表達式來拆分此字元串。
boolean startsWith(String prefix)
測試此字元串是否以指定的前綴開始。
boolean startsWith(String prefix, int toffset)
測試此字元串是否以指定前綴開始,該前綴以指定索引開始。
CharSequence subSequence(int beginIndex, int endIndex)
返回一個新的字元序列,它是此序列的一個子序列。
String substring(int beginIndex)
返回一個新的字元串,它是此字元串的一個子字元串。
String substring(int beginIndex, int endIndex)
返回一個新字元串,它是此字元串的一個子字元串。
char[] toCharArray()
將此字元串轉換為一個新的字元數組。
String toLowerCase()
使用默認語言環境的規則將此 String 中的所有字元都轉換為小寫。
String toLowerCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字元都轉換為小寫。
String toString()
返回此對象本身(它已經是一個字元串!)。
String toUpperCase()
使用默認語言環境的規則將此 String 中的所有字元都轉換為大寫。
String toUpperCase(Locale locale)
使用給定的 Locale 規則將此 String 中的所有字元都轉換為大寫。
String trim()
返回字元串的副本,忽略前導空白和尾部空白。
static String valueOf(boolean b)
返回 boolean 參數的字元串表示形式。
static String valueOf(char c)
返回 char 參數的字元串表示形式。
static String valueOf(char[] data)
返回 char 數組參數的字元串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 數組參數的特定子數組的字元串表示形式。
static String valueOf(double d)
返回 double 參數的字元串表示形式。
static String valueOf(float f)
返回 float 參數的字元串表示形式。
static String valueOf(int i)
返回 int 參數的字元串表示形式。
static String valueOf(long l)
返回 long 參數的字元串表示形式。
static String valueOf(Object obj)
返回 Object 參數的字元串表示形式。
從類 java.lang.Object 繼承的方法
clone, finalize, getClass, notify, notifyAll, wait, wait, wait

『拾』 c語言string的用法大全

C語言是一門面向過程的、抽象化的通用程序設計語言,廣泛應用於底層開發。C語言能以簡易的方式編譯、處理低級存儲器。C 語言string的用法有哪些呢,請看看下面我為你整理 總結 的c語言string的用法大全_C語言中string使用 方法 。

c語言string的用法

函數原型:char *strp(const char *s)

函數功能:字元串拷貝,目的空間由該函數分配

函數返回:指向拷貝後的字元串指針

參數說明:src-待拷貝的源字元串

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

char*p_str,*string="abcde";

p_str=strp(string);

printf("%s",p_str);

free(p_str);

return0;

}

@函數名稱:strcpy

函數原型:char* strcpy(char* str1,char* str2);

函數功能:把str2指向的字元串拷貝到str1中去

函數返回:返回str1,即指向str1的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10];

char*str1="abcdefghi";

strcpy(string,str1);

printf("thestringis:%s ",string);

return0;

}

@函數名稱:strncpy

函數原型:char *strncpy(char *dest, const char *src,intcount)

函數功能:將字元串src中的count個字元拷貝到字元串dest中去

函數返回:指向dest的指針

參數說明:dest-目的字元串,src-源字元串,count-拷貝的字元個數

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's

chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's

puts(dest);

strncpy(dest,src,10);

puts(dest);

return0;

}

輸出:

[cpp] view plain

/*******************************************

aaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbaaaaaaaaaa

*******************************************/

注意:strncpy只復制指定長度的字元,不會自動在末尾加''。若指定長度超過源字元串長度,不夠的部分補『』,

@函數名稱:strcat

函數原型:char* strcat(char * str1,char * str2);

函數功能:把字元串str2接到str1後面,str1最後的''被取消

函數返回:str1

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charbuffer[80];

strcpy(buffer,"Hello");

strcat(buffer,"world");

printf("%s ",buffer);

return0;

}

@函數名稱:strncat

函數原型:char *strncat(char *dest, const char *src, size_t maxlen)

函數功能:將字元串src中前maxlen個字元連接到dest中

函數返回:

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

charbuffer[80];

intmain()

{

strcpy(buffer,"Hello");

strncat(buffer,"world",8);

printf("%s ",buffer);

strncat(buffer,"*************",4);

printf("%s ",buffer);

return0;

}

注意:與strncpy不同的是,strncat會自動在末尾加『』,若指定長度超過源字元串長度,則只復制源字元串長度即停止

@函數名稱:strcmp

函數原型:int strcmp(char * str1,char * str2);

函數功能:比較兩個字元串str1,str2.

函數返回:str1<str2,返回負數;str1=str2,返回 0;str1>str2,返回正數.

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*buf1="aaa",*buf2="bbb",*buf3="ccc";

intptr;

ptr=strcmp(buf2,buf1);

if(ptr>0)

printf("buffer2isgreaterthanbuffer1 ");

else

printf("buffer2islessthanbuffer1 ");

ptr=strcmp(buf2,buf3);

if(ptr>0)

printf("buffer2isgreaterthanbuffer3 ");

else

printf("buffer2islessthanbuffer3 ");

return0;

}

@函數名稱:strncmp

函數原型:int strncmp(char *str1,char *str2,int count)

函數功能:對str1和str2中的前count個字元按字典順序比較

函數返回:小於0:str1<str2,等於0:str1=str2,大於0:str1>str2

參數說明:str1,str2-待比較的字元串,count-比較的長度

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstr1[]="aabbc";//

charstr2[]="abbcd";//

//為使測試程序更簡練,此處假定了strncmp只返回-1,0,1三個數

charres_info[]={'<','=','>'};

intres;

//前1個字元比較

res=strncmp(str1,str2,1);

printf("1:str1%cstr2 ",res_info[res+1]);

//前3個字元比較

res=strncmp(str1,str2,3);

printf("3:str1%cstr2 ",res_info[res+1]);

}

輸出:

[cpp] view plain

/****************************************

1:str1=str2

3:str1<str2

*****************************************/

@函數名稱:strpbrk

函數原型:char *strpbrk(const char *s1, const char *s2)

函數功能:得到s1中第一個「同時也出現在s2中」字元的位置指針

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*p="Findallvowels";

p=strpbrk(p+1,"aeiouAEIOU");

while(p)

{

printf("%s ",p);

p=strpbrk(p+1,"aeiouAEIOU");

}

return0;

}

輸出:

[cpp] view plain

/**************************************

indallvowels

allvowels

owels

els

**************************************/

@函數名稱:strcspn

函數原型:int strcspn(const char *s1, const char *s2)

函數功能:統計s1中從頭開始直到第一個「來自s2中的字元」出現的長度

函數返回:長度

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

printf("%d ",strcspn("abcbcadef","cba"));

printf("%d ",strcspn("xxxbcadef","cba"));

printf("%d ",strcspn("123456789","cba"));

return0;

}

輸出:

[cpp] view plain

/************************

0

3

9

************************/

@函數名稱:strspn

函數原型:int strspn(const char *s1, const char *s2)

函數功能:統計s1中從頭開始直到第一個「不來自s2中的字元」出現的長度

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[html] view plain

#include<stdio.h>

#include<string.h>

#include<alloc.h>

intmain()

{

printf("%d ",strspn("abcbcadef","cba"));

printf("%d ",strspn("xxxbcadef","cba"));

printf("%d ",strspn("123456789","cba"));

return0;

}

輸出:

[cpp] view plain

/************************

6

0

0

************************/

@函數名稱:strchr

函數原型:char* strchr(char* str,char ch);

函數功能:找出str指向的字元串中第一次出現字元ch的位置

函數返回:返回指向該位置的指針,如找不到,則返回空指針

參數說明:str-待搜索的字元串,ch-查找的字元

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*str="Thisisastring!";

charch;

char*p;

while(1)

{

printf("Pleaseinputachar:");

ch=getchar();

p=strchr(str,ch);

if(p)

printf("%cisthe%dcharacterof"%s" ",ch,(int)(p-str+1),str);

else

printf("Notfound! ");

printf("PressESCtoquit! ");

if(27==getch())

break;

fflush(stdin);

}

return0;

}

運行結果:

[cpp] view plain

/********************************************

Pleaseinputachar:i

iisthe3characterof"Thisisastring!"

PressESCtoquit!

Pleaseinputachar:l

Notfound!

PressESCtoquit!

Pleaseinputachar:s

sisthe4characterof"Thisisastring!"

PressESCtoquit!

**********************************************/

@函數名稱:strrchr

函數原型:char *strrchr(const char *s, int c)

函數功能:得到字元串s中最後一個含有c字元的位置指針

函數返回:位置指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstring[15];

char*ptr,c='r';

strcpy(string,"Thisisastring");

ptr=strrchr(string,c);

if(ptr)

printf("Thecharacter%cisatposition:%d",c,ptr-string);

else

printf("Thecharacterwasnotfound");

return0;

}

@函數名稱:strstr

函數原型:char* strstr(char* str1,char* str2);

函數功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)

函數返回:返回該位置的指針,如找不到,返回空指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;

ptr=strstr(str1,str2);

printf("Thesubstringis:%s ",ptr);

return0;

}

輸出:

The substringis:Watcom C/C++

@函數名稱:strrev

函數原型:char *strrev(char *s)

函數功能:將字元串中的所有字元顛倒次序排列

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charforward[]="string";//原文中定義為char*是不對的,指向代碼段的指針內容是不可變的

printf("Beforestrrev():%s",forward);

strrev(forward);

printf("Afterstrrev():%s",forward);

return0;

}

輸出:

[cpp] view plain

/************************************

Beforestrrev():string

Afterstrrev():gnirts

************************************/

@函數名稱:strnset

函數原型:char *strnset(char *s, int ch, size_t n)

函數功能:將字元串s中前n個字元設置為ch的值

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";

charletter='x';

printf("stringbeforestrnset:%s ",string);

strnset(string,letter,10);

printf("stringafterstrnset:%s ",string);

return0;

}

輸出:

[cpp] view plain

/*************************************************

stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa

stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa

*************************************************/

@函數名稱:strset

函數原型:char *strset(char *s, int ch)

函數功能:將字元串s中所有字元設置為ch的值

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[10]="123456789";

charsymbol='c';

printf("Beforestrset():%s",string);

strset(string,symbol);

printf("Afterstrset():%s",string);

return0;

}

@函數名稱:strtok

函數原型:char *strtok(char *s1, const char *s2)

函數功能:分解s1字元串為用特定分隔符分隔的多個字元串(一般用於將英文句分解為單詞)

函數返回:字元串s1中首次出現s2中的字元前的子字元串指針

參數說明:s2一般設置為s1中的分隔字元

規定進行子調用時(即分割s1的第二、三及後續子串)第一參數必須是NULL

在每一次匹配成功後,將s1中分割出的子串位置替換為NULL(摘下鏈中第一個環),因此s1被破壞了

函數會記憶指針位置以供下一次調用

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*p;

char*buffer;

char*delims={".,"};

buffer=strp("Findwords,allofthem.");

printf("%s ",buffer);

p=strtok(buffer,delims);

while(p!=NULL){

printf("word:%s ",p);

p=strtok(NULL,delims);

}

printf("%s ",buffer);

return0;

}//根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔

PS:根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔

@函數名稱:strupr

函數原型:char *strupr(char *s)

函數功能:將字元串s中的字元變為大寫

函數返回:

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//會影響原字元串的內存,用char[]來聲明

ptr=strupr(string);

printf("%s",ptr);

return0;

}

@函數名稱:strlwr

函數原型:char *strlwr(char *s)

函數功能:將字元串中的字元變為小寫字元

函數返回:指向s的指針

參數說明:

所屬文件:<string.h>

[cpp] view plain

#include<string.h>

intmain()

{

charstr[]="HOWTOSAY";

printf("%s",strlwr(str));

return0;

}

@函數名稱:strerror

函數原型:char *strerror(int errnum)

函數功能:得到錯誤信息的內容信息

函數返回:錯誤提示信息字元串指針

參數說明:errnum-錯誤編號

所屬文件:<string.h>

[cpp] view plain

#include<stdio.h>

#include<errno.h>

intmain()

{

char*buffer;

buffer=strerror(errno);

printf("Error:%s",buffer);

return0;

}

@函數名稱:memcpy

函數原型:void *memcpy(void *dest, const void *src, size_t n)

函數功能:字元串拷貝

函數返回:指向dest的指針

參數說明:src-源字元串,n-拷貝的最大長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

charsrc[]="******************************";

chardest[]="";

char*ptr;

printf("destinationbeforememcpy:%s ",dest);

ptr=memcpy(dest,src,strlen(src));

if(ptr)

printf("destinationaftermemcpy:%s ",dest);

else

printf("memcpyfailed");

return0;

}

輸出:

[cpp] view plain

/*************************************************************

destinationbeforememcpy:

destinationaftermemcpy:******************************456709

**************************************************************/

@函數名稱:memccpy

函數原型:void *memccpy(void *dest, const void *src, int c, size_t n)

函數功能:字元串拷貝,到指定長度或遇到指定字元時停止拷貝

函數返回:

參數說明:src-源字元串指針,c-中止拷貝檢查字元,n-長度,dest-拷貝底目的字元串指針

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

char*src="Thisisthesourcestring";

chardest[50];

char*ptr;

ptr=memccpy(dest,src,'c',strlen(src));

if(ptr)

{

*ptr='';

printf("Thecharacterwasfound:%s",dest);

}

else

printf("Thecharacterwasn'tfound");

return0;

}

輸出:

[cpp] view plain

/*****************************************

Thecharacterwasfound:Thisisthesourc

*****************************************/

PS:指定字元被復制到dest中,memccpy返回了dest中指定字元的下一處的地址,返回NULL表示未遇到指定字元

@函數名稱:memchr

函數原型:void *memchr(const void *s, int c, size_t n)

函數功能:在字元串中第開始n個字元中尋找某個字元c的位置

函數返回:返回c的位置指針,返回NULL時表示未找到

參數說明:s-要搜索的字元串,c-要尋找的字元,n-指定長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

charstr[17];

char*ptr;

strcpy(str,"Thisisastring");

ptr=memchr(str,'r',strlen(str));

if(ptr)

printf("Thecharacter'r'isatposition:%d",ptr-str);

else

printf("Thecharacterwasnotfound");

return0;

}

@函數名稱:memcmp

函數原型:int memcmp(const void *s1, const void *s2,size_t n)

函數功能:按字典順序比較兩個串s1和s2的前n個位元組

函數返回:<0,=0,>0分別表示s1<,=,>s2

參數說明:s1,s2-要比較的字元串,n-比較的長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*buf1="ABCDE123";

char*buf2="abcde456";

intstat;

stat=memcmp(buf1,buf2,5);

printf("Thestringstoposition5are");

if(stat)printf("not");

printf("thesame ");

return0;

}

@函數名稱:memicmp

函數原型:int memicmp(const void *s1, const void *s2, size_t n)

函數功能:按字典順序、不考慮字母大小寫對字元串s1,s2前n個字元比較

函數返回:<0,=0,>0分別表示s1<,=,>s2

參數說明:s1,s2-要比較的字元串,n-比較的長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<stdio.h>

#include<string.h>

intmain()

{

char*buf1="ABCDE123";

char*buf2="abcde456";

intstat;

stat=memicmp(buf1,buf2,5);

printf("Thestringstoposition5are");

if(stat)printf("not");

printf("thesame");

return0;

}

輸出:

[cpp] view plain

/**************************************

***************************************/

@函數名稱:memmove

函數原型:void *memmove(void *dest, const void *src, size_t n)

函數功能:字元串拷貝

函數返回:指向dest的指針

參數說明:src-源字元串,n-拷貝的最大長度

所屬文件:<string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

intmain()

{

chardest[40]="";

printf("destinationpriortomemmove:%s ",dest);

memmove(dest+1,dest,35);

printf("destinationaftermemmove:%s",dest);

return0;

}

PS:與memcpy不同的是,memmove可以處理目的字元串與源字元串地址空間出現重疊的情況,可保證待復制的內容不被破壞。

@函數名稱: memset

函數原型: void *memset(void *s, int c, size_t n)

函數功能: 字元串中的n個位元組內容設置為c

函數返回:

參數說明: s-要設置的字元串,c-設置的內容,n-長度

所屬文件: <string.h>,<mem.h>

[cpp] view plain

#include<string.h>

#include<stdio.h>

#include<mem.h>

intmain()

{

charbuffer[]="Helloworld";

printf("Bufferbeforememset:%s/n",buffer);

memset(buffer,'*',strlen(buffer)-1);

printf("Bufferaftermemset:%s",buffer);

return0;

}


c語言string的用法大全相關 文章 :

★ c語言string的用法

★ c語言的用法

★ Linux C語言字元與字元串處理

★ c語言中strcmp的用法

★ c語言大括弧的用法

★ c語言位運算符的用法

★ c語言char的用法

★ c語言中sort的用法詳解

★ c語言中int的用法

★ c語言map的用法

閱讀全文

與string使用方法相關的資料

熱點內容
中式棉襖製作方法圖片 瀏覽:63
五菱p1171故障碼解決方法 瀏覽:858
男士修護膏使用方法 瀏覽:546
電腦圖標修改方法 瀏覽:607
濕氣怎麼用科學的方法解釋 瀏覽:537
910除以26的簡便計算方法 瀏覽:805
吹東契奇最簡單的方法 瀏覽:704
對腎臟有好處的食用方法 瀏覽:98
電腦四線程內存設置方法 瀏覽:512
數字電路通常用哪三種方法分析 瀏覽:13
實訓課程的教學方法是什麼 瀏覽:525
苯甲醇乙醚鑒別方法 瀏覽:82
蘋果手機微信視頻聲音小解決方法 瀏覽:700
控制箱的連接方法 瀏覽:75
用什麼簡單的方法可以去痘 瀏覽:789
快速去除甲醛的小方法你知道幾個 瀏覽:803
自行車架尺寸測量方法 瀏覽:124
石磨子的製作方法視頻 瀏覽:152
行善修心的正確方法 瀏覽:403
土豆燉雞湯的正確方法和步驟 瀏覽:276