字符串中怎么截取指定字符的前面的字符和后面的字符
\u5b57\u7b26\u4e32\u4e2d\u600e\u4e48\u622a\u53d6\u6307\u5b9a\u5b57\u7b26\u7684\u524d\u9762\u7684\u5b57\u7b26\u548c\u540e\u9762\u7684\u5b57\u7b26var str = 'abcd';
str.substr(0, str.indexOf('b') + 1);
"ab"
var str = 'abcd';
str.substr(str.indexOf('b') + 1);
"cd"
String str = "afhakhfakshfka\u6307\u5b9a\u5b57\u7b26adasfasfa"; System.out.println(str.replaceAll("[^a-zA-Z]", "")); \u770b\u4e0b\u662f\u4e0d\u662f\u4f60\u8981\u7684\u7ed3\u679c\u5475\u5475
1)rangeOfString:截取指定字符串的长度;2) substringToIndex:7:截取从0 索引到指定索引(7)长度的字符串
3) substringFromIndex:9:截取从指定索引(9)到末尾长度的字符串
4) NSRange range = NSMakeRange(0,7);截取从指定索引(0)到指定(7)长度的字符串
5) substringWithRange:range:截取从指定索引(0)到指定(7)长度的字符串
6 )isEqualToString:@"p://www":比较字符串包含关系,区分大小写
7) stringWithFormat:格式化字符串。
void findSubString(){
NSString *filename = @"config.ini";
NSString *escapedPath = @"/Users/config.ini";
//在escapedPath中查找filename
//NSRange iStart = [escapedPath rangeOfString :filename];
NSRange iStart = [escapedPath rangeOfString: filename options:NSCaseInsensitiveSearch];
// NSLog(@"iStart length is %@:",iStart.length);
// NSLog(@"iStart location is %@:",iStart.location);
if (iStart.length > 0){
//获取从escapedPath开始位置到iStart.location-1长度的子字符串
NSString *subStr = [escapedPath substringToIndex:iStart.location-1];
NSLog(@"subStr:%@",subStr);
//获取从escapedPath从iStart.location+1开始到末尾的子字符串
NSString *extension = [escapedPath substringFromIndex:iStart.location]; //往后久在加上搜索字符串的长度
NSLog(@"extension:%@",extension);
NSString *url =@"http://www.sevenuc.com";
NSRange range = NSMakeRange(3,7);
NSString* prefix = [url substringWithRange:range];
NSLog(@"prefix is:%@",prefix);
if([prefix isEqualToString:@"p://www"])
{
NSLog(@"prefix is equal %@",prefix);
}
else {
NSLog(@"prefix is not equal %@",prefix);
}
//获取url从0开始共7个字符的子字符串
if ( [prefix isEqualToString:@"http://"] )
NSLog(@"http prefix found");
}
}
void formatString(){
int documentId=100;
NSString *documentFileName=@"test.doc";
NSString *requestSring=[NSString stringWithFormat:@"<files><fileid>%d</fileid><filename>%@</filename></files>",documentId,documentFileName];
NSLog(@"requestString:%@",requestSring);
}
绛旓細渚嬪锛宍str.substring(1, 4)` 浼鎴彇 "ell"锛岃 `str.substring(3, 5)` 鍒欐槸 "lo"銆傚悓鏍凤紝濡傛灉 startIndex 鎴 endIndex 瓒呭嚭瀛楃涓鑼冨洿锛屼細寮曞彂寮傚父銆傚湪瀹為檯浣跨敤涓紝杩欎袱绉嶅舰寮忕殑substring鏂规硶閮借兘甯姪鎴戜滑鏈夋晥鍦版埅鍙栧拰澶勭悊瀛楃涓诧紝浣嗗姟蹇呯‘淇濇彁渚涚殑绱㈠紩鍊煎湪鏈夋晥鑼冨洿鍐咃紝浠ラ伩鍏嶈繍琛屾椂閿欒銆
绛旓細key") 鑾峰彇鍑虹幇key瀛楃涓茬殑绗竴涓綅缃紝杩欓噷瑕佷繚璇佸墠闈㈡病鏈夎窡KEY閲嶅 String left = s.substring(index) 鎴彇KEY鍑虹幇浣嶇疆鍓嶉潰鐨勫瓧绗︿覆 String right = s.sbustring(index+3) 鍚庨潰鐨勫瓧绗︿覆锛 +3 鏄痥ey鐨勯暱搴︿负3 (KEY鐨勫奸暱搴)String[] strs = str.split(Pattern.quote("浣鎸囧畾鐨勫瓧绗"));
绛旓細1銆佸皢鏂板缓濂界殑琛╝aaa锛屽~鍏呴渶瑕佹煡璇㈢殑鏁版嵁浠"_"鎴栬","涓哄垎闅旂鐨勪袱鏉℃暟鎹2銆佸厛鎸慖D涓1鐨勬暟鎹繘琛屾埅鍙栨煡璇㈡紨绀恒3銆佸鏋滃凡鐭鎴彇瀛楃鐨闀垮害鍜岀壒娈婂瓧绗︿綅缃紝灏卞彲浠ョ敤substring()鍑芥暟鏉ヤ粠瀛楃涓鐨勫紑濮嬩綅缃紝鎴彇鍒扮壒娈婂瓧绗﹀嚭鐜扮殑浣嶇疆銆4銆佷娇鐢╟harindex()鍑芥暟鏉ヨ繑鍥炵壒娈婂瓧绗﹀嚭鐜扮殑浣嶇疆锛屾煡璇㈢粨鏋滃強璇彞...
绛旓細Excel涓鎴彇瀛楃涓茬殑鏂规硶鍙婃楠わ細1. left鍑芥暟鐢ㄦ潵瀵瑰崟鍏冩牸鍐呭杩涜鎴彇銆備粠宸﹁竟绗竴涓瓧绗﹀紑濮嬫埅鍙栵紝鎴彇鎸囧畾鐨闀垮害銆2. left鍑芥暟鐨勮娉曟牸寮忥紝=left(text,num_chars)锛宼ext浠h〃鐢ㄦ潵鎴彇鐨勫崟鍏冩牸鍐呭銆俷um_chars浠h〃浠庡乏寮濮鎴彇鐨勫瓧绗鏁般3. 濡傚浘鎵绀猴紝瀵逛簬涓闀夸覆鍦板潃涓彁鍙栫渷浠姐傝緭鍏ュ叕寮=LEFT(A1...
绛旓細excel鎬庢牱鑾峰彇鍗曞厓鏍间腑鐗瑰畾瀛楃鍓嶉潰鎴栬呭悗闈㈢殑鍐呭 1銆佸厛涓句緥锛岃鎻愬彇#鍚庨潰鐨勫瓧绗︹淪DK鈥濆埌B鍒楋紱2銆佸湪B2涓緭鍏ュ叕寮忥細=FIND("#",A2)锛岃繑鍥#鍦瀛楃涓蹭腑鐨浣嶇疆锛#鍦ˋ2鍗曞厓鏍兼枃鏈腑鏄6涓瓧绗︼紱3銆丗IND()鍑芥暟鏌ユ壘绗竴鍙傛暟鍦ㄧ浜屽弬鏁颁腑鐨勪綅缃紝鏌ユ壘鈥淏鈥濆湪鈥淎BCD鈥濅腑鏄鍑犱釜瀛楃锛岀涓鍙傛暟鏄...
绛旓細1銆侀鍏堟墦寮excel鎵撳紑鏁版嵁鐢↙EFT鍛戒护锛屽彲浠ュ彇鍗曞厓鏍煎唴宸﹁竟鍑犱綅瀛楃銆2銆佺敤RIGHT鍙互鍙栧崟鍏冩牸鍙宠竟鍑犱綅瀛楃銆3銆乵id鍙互鐢ㄦ潵鍙栧崟鍏冩牸涓棿閮ㄥ垎瀛楃銆傚鍥撅紝鍙栦粠绗5浣嶅紑濮嬬殑2涓瓧绗︺4銆佸鍥鹃渶瑕佺敤FIND鍑芥暟鏉ユ煡鎵惧瓧绗﹁捣濮嬬偣銆傝娉ㄦ剰鐨勬槸锛屽彧鏌ユ壘鐨勬槸绗竴涓瓧绗﹀紑濮嬬殑鍦版柟锛屽鏋滄湁涓ょ浉鐩稿悓鐨勫瓧绗︼紝绗2涓笉浼...
绛旓細/*** Sql Server涓鎴彇瀛楃涓茬殑甯哥敤鏂规硶 ***/--1銆丩EFT()鏂规硶---鍑芥暟璇存槑---1锛夎娉曪細LEFT(character,integer) --2锛変粙缁嶏細鍙傛暟1锛氳鎴彇鐨勫瓧绗︿覆锛屽弬鏁2锛氭埅鍙栧瓧绗︿釜鏁--3锛変娇鐢細--杩斿洖浠庡瓧绗︿覆宸﹁竟寮濮鎸囧畾涓暟鐨勫瓧绗--select LEFT('SqlServer_2008',3)--4锛夎繑鍥烇細Sql--1銆丷IGHT...
绛旓細鍏蜂綋姝ラ濡備笅锛1銆侀鍏堝噯澶囦竴寮爀xcel琛ㄦ牸鍜岄儴鍒嗕复鏃舵祴璇曟暟鎹紝鎴戜滑甯屾湜鍙栫壒瀹氬瓧绗︹淜鈥濅箣鍓嶇殑瀛楁锛屽鈥滆澶囩淮淇濄佲滃鐢典繚鍏烩濈瓑锛堜负浜嗗皢瑙i鎬濊矾鏇存竻鏅扮殑灞曠ず锛屾垜浠皢涓棿姝ラ涔熶綔涓哄崟鐙殑鍒楄繘琛屽鐞嗭級锛2銆佸厛鏌ユ壘鐗瑰畾瀛楃鐨浣嶇疆锛屽湪B2鍗曞厓鏍间腑杈撳叆锛屽洖杞︾‘璁わ紱3銆佸洖杞﹀悗锛屼細鍙戠幇B2鍗曞厓鏍煎凡缁忚缃垚鍔...
绛旓細@STR2 AS A2 --SELECT杈撳嚭 浠ヤ笂鏄负浜嗕究浜庝綘鐞嗚В鎵浠ュ啓寰楃儲鐞愶紝涓嬮潰鏄疄鐢ㄥ啓娉曪細DECLARE @STRALL VARCHAR(20) -- SET @STRALL = '350X01220' ---涓婇潰涓ゅ彞璇锋牴鎹綘鐨勫疄鎯呭幓淇锛屾瘮濡備綘瑕佹槸鏌ユ煇琛ㄤ腑鐨勫瓧娈碉紝閭d笂闈袱鍙ュ氨涓嶈浜--鐩存帴鐢ㄥ瓧娈靛悕浠f崲鍘讳笅闈㈢殑@STRALLSELECT LEFT(@STRALL,CHAR...
绛旓細鍙互鐩存帴璋冪敤subString()鏂规硶鏉ヨ繘琛瀛楃涓叉埅鍙銆俻ublic String substring锛坕nt beginIndex锛宨nt endIndex锛夛紝杩斿洖涓涓柊瀛楃涓诧紝瀹冩槸姝瀛楃涓茬殑涓涓瓙瀛楃涓层傝瀛愬瓧绗︿覆浠鎸囧畾鐨 beginIndex 澶勫紑濮嬶紝鐩村埌绱㈠紩 endIndex - 1 澶勭殑瀛楃銆傚洜姝わ紝璇ュ瓙瀛楃涓茬殑闀垮害涓 endIndex-beginIndex銆