在C或C++中有没有对字符串截取前某几位的函数 c++ 如何截取字符串的后几位
\u5728C\u6216C++\u4e2d\u6709\u6ca1\u6709\u5bf9\u5b57\u7b26\u4e32\u622a\u53d6\u524d\u67d0\u51e0\u4f4d\u7684\u51fd\u6570\u622a\u53d6\u524d\u51e0\u4f4d\u7684\u610f\u601d\u662f\u622a\u53d6\u5b57\u7b26\u4e32\u524d\u67d0\u51e0\u4e2a\u5b57\u7b26\u5417\uff1f\u5982\u679c\u662f\u7684\u8bdd\uff0cC\u8bed\u8a00\u4e2d\u6709:
char * strncpy ( char * destination, const char * source, size_t num );
\u53ef\u4ee5\u5c06\u524d\u51e0\u4f4d\u590d\u5236\u5230\u65b0\u7684\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u3002
C++\u4e2d\u6709:
string substr (size_t pos = 0, size_t len = npos) const;
\u6d4b\u8bd5\u6e90\u7801\u89c1\u4ee5\u4e0b\u94fe\u63a5\uff1a
strncpy: http://www.cplusplus.com/reference/cstring/strncpy/
string::substr: http://www.cplusplus.com/reference/string/string/substr/
\u5e0c\u671b\u80fd\u591f\u5e2e\u5230\u4f60\u3002
char *substrend(char * str, int n);
char substrend(char *str, int n)
{
char * substr = (char*) malloc (n+1);
int length = strlen(str);
if(n >= length)//\u82e5\u622a\u53d6\u957f\u5ea6\u5927\u4e8e\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u5219\u76f4\u63a5\u622a\u53d6\u5168\u90e8\u5b57\u7b26\u4e32
{
strcpy(substr, str);
return substr;
}
int k = 0;
for(int i = strlen(str) - n - 1; i < strlen(str); i++)
{
substr[k++] = str[i];
}
substr[k] = '\0';
return substr;
}
str.substr(0, 3); //0表示str的起始位置,就是第一个字符的位置;3表示截取几个字符
并返回截取后的新的字符串
可以用substr函数
绛旓細string str;str.substr(0, 3); //0琛ㄧずstr鐨勮捣濮嬩綅缃紝灏辨槸绗竴涓瓧绗︾殑浣嶇疆锛3琛ㄧず鎴彇鍑犱釜瀛楃 骞惰繑鍥炴埅鍙栧悗鐨勬柊鐨瀛楃涓
绛旓細杩欎釜浠g爜灏嗘妸鏁存暟123456杞崲涓轰竴涓瀛楃涓锛屽苟灏嗗畠瀛樺偍鍦ㄤ竴涓暱搴︿负10鐨勫瓧绗︽暟缁勪腑銆傝浆鎹㈢殑鏍煎紡涓哄彧淇濈暀鍓嶄笁浣嶏紝鍗"123"銆傚洜姝わ紝杈撳嚭鎴柇灏嗕細鍙戠敓銆傛荤粨 杈撳叆鎴柇鍜杈撳嚭鎴柇閮芥槸C璇█涓潪甯稿父瑙佺殑鐜拌薄銆備簡瑙h繖浜涚幇璞¤兘澶熷府鍔╁紑鍙戣呯紪鍐欐洿鍔犲畨鍏ㄥ拰鍙潬鐨勪唬鐮併傚挨鍏舵槸鍦ㄤ笌鐢ㄦ埛浜や簰鐨勭▼搴忎腑锛岃緭鍏ユ埅鏂拰杈撳嚭鎴柇...
绛旓細瑕佽揪鍒拌繖鏍风殑鏁堟灉锛岀敤sscanf鐪嬫潵鏄悶涓嶅畾浜嗭紝鍙兘鑷繁鍐欏嚱鏁般備笅闈㈢殑鍑芥暟鐪嬫槸鍚︽弧瓒抽渶姹傦紝浣犲彲浠ュ湪鍏ㄥ眬鍙橀噺str[]閲屽姞浠绘剰澶氱殑璺緞锛屼絾鏈変竴鐐规敞鎰忕殑鏄紝瀛楃\鍦ㄤ覆閲蹇呴』杞箟涓篭\锛岃矾寰勯暱搴︿笉瓒呰繃128浣嶅嵆鍙俰nclude <stdio.h> include <string.h> include <stdlib.h> char str[] = "clamscan c:\...
绛旓細C璇█娌℃湁鐩稿簲杩愮畻绗︼紝鍙彁渚涗簡涓浜涘嚱鏁版潵澶勭悊瀛楃涓 鍦–++璇█涓紝鎻愪緵浜唖tring绫伙紝杩欎釜绫讳腑瀵+绠楃杩涜浜嗛噸杞斤紝鍙互瀵瑰瓧绗︿覆杩涜鎷兼帴
绛旓細strcmp(p, p1) 姣旇緝瀛楃涓 strcasecmp蹇界暐澶у皬鍐欐瘮杈冨瓧绗︿覆 strncmp(p, p1, n) 姣旇緝鎸囧畾闀垮害瀛楃涓 strchr(p, c) 鍦ㄥ瓧绗︿覆涓煡鎵炬寚瀹氬瓧绗 strrchr(p, c) 鍦ㄥ瓧绗︿覆涓弽鍚戞煡鎵 strstr(p, p1) 鏌ユ壘瀛楃涓 strpbrk(p, p1) 浠ョ洰鏍囧瓧绗︿覆鐨勬墍鏈夊瓧绗︿綔涓洪泦鍚堬紝鍦ㄥ綋鍓嶅瓧绗︿覆鏌ユ壘璇ラ泦鍚堢殑浠讳竴鍏冪礌 str...
绛旓細C璇█瑙勫畾鍙兘閫愪釜寮曠敤鏁扮粍鍏冪礌锛岃屼笉鑳戒竴娆″紩鐢ㄦ暣涓暟缁勩瀵逛簬瀛楃鏁扮粍锛屽彲浠ュ皢鏁翠釜瀛楃涓蹭竴娆¤緭鍏ユ垨杈撳嚭銆傚瓧绗︽暟缁勭敤浜庡瓨鏀惧瓧绗鎴栧瓧绗︿覆锛屽瓧绗︽暟缁勪腑鐨勪竴涓厓绱犲瓨鏀句竴涓瓧绗︼紝瀹冨湪鍐呭瓨涓崰鐢ㄤ竴涓瓧鑺傘侰璇█涓病鏈夊瓧绗︿覆绫诲瀷锛屽瓧绗︿覆鏄瓨鏀惧湪瀛楃鍨嬫暟缁勪腑鐨勩
绛旓細C/C++ string搴擄紙string.h锛夋彁渚涗簡鍑犱釜瀛楃涓叉煡鎵惧嚱鏁帮紝濡備笅锛歮emchr 鍦ㄦ寚瀹氬唴瀛橀噷瀹氫綅缁欏畾瀛楃 strchr 鍦ㄦ寚瀹氬瓧绗︿覆閲屽畾浣嶇粰瀹氬瓧绗 strcspn 杩斿洖鍦ㄥ瓧绗︿覆str1閲屾壘鍒板瓧绗︿覆str2閲岀殑浠绘剰涓涓瓧绗︿箣鍓嶅凡鏌ユ壘鐨勫瓧绗︽暟閲 strrchr 鍦ㄥ瓧绗︿覆閲瀹氫綅缁欏畾瀛楃鏈鍚庝竴娆″嚭鐜扮殑浣嶇疆 strpbrk 鍦ㄥ瓧绗︿覆str1閲屽畾浣嶅瓧绗︿覆...
绛旓細C璇█鐨勫瓧绗︿覆涓烘暟缁勫舰寮忥紝鑰屾暟缁勭殑鍚嶇О鍙互琛ㄧず鍏跺湴鍧锛屾晠瀵瑰瓧绗︿覆杈撳叆涓嶉渶瑕佸姞鍦板潃绗︺傝屽崟鐙殑瀛楃銆佹暣鍨嬪彉閲忕瓑鐨勫悕绉颁笉鑳界洿鎺ヨ〃绀哄湴鍧锛屾晠闇瑕佸姞鍦板潃绗︺傛暟鎹被鍨 瀛楃涓叉暟鎹被鍨嬫槸寤烘ā鍦ㄥ舰寮忓瓧绗︿覆鐨勬兂娉曚笂鐨勬暟鎹被鍨嬨傚瓧绗︿覆鏄嚑涔庡湪鎵鏈夌紪绋嬭瑷涓彲浠ュ疄鐜扮殑闈炲父閲嶈鍜屾湁鐢ㄧ殑鏁版嵁绫诲瀷銆傚湪鏌愪簺璇█涓...
绛旓細娌℃湁瀛楃涓绫诲瀷锛屼絾鏄C璇█涓瓧绗︿覆瀹為檯灏辨槸澶氫釜瀛楃閾炬帴鍦ㄤ竴璧凤紝鎵浠ュ彲浠ョ敤瀛楃鏁扮粍鎴栬鎸囬拡銆傚彲浠ヨ繖鏍峰畾涔 char s[]="this is a string";C璇█涔熸湁string.h澶存枃浠讹紝浣跨敤杩欎釜澶存枃浠跺彲浠ョ洿鎺ヨ繖鏍 string s="this is a string";鏈涢噰绾
绛旓細娌℃鏌鍜缂栬瘧杩囷紝宸笉澶氬氨鏄繖鏍蜂簡銆傛湁浜嗚繖浜涘嚱鏁板氨鍙互鍒ゆ柇绫讳技浣忓潃澶4涓瓧鑺傛槸鍚︽槸鈥滃箍宸炩濄佹у埆灞炴ф槸鍚︿负鈥滃コ鈥濈瓑杩欎簺鏉′欢浜嗐俻s锛c涓鏂囦欢鐨勬搷浣滐細stdio.h澶存枃浠朵腑瀹氫箟浜嗗鏂囦欢鐨勫紩鐢ㄧ被鍨嬶細FILE 鍙婂叾鍩烘湰鎿嶄綔锛歩nclude <stdio.h> ...FILE *filePointer filePointer = fopen("c://student....