java中怎么按字符分割字符串
java\u5982\u4f55\u5206\u5272\u5b57\u7b26\u4e32substring\u8bb2\u89e3\uff1a
s\uff1ds.substring(int begin);\u622a\u53d6\u6389s\u4ece\u9996\u5b57\u6bcd\u8d77\u957f\u5ea6\u4e3abegin\u7684\u5b57\u7b26\u4e32\uff0c\u5c06\u5269\u4f59\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9s\uff1b
s\uff1ds.substring(int begin\uff0cint end);\u622a\u53d6s\u4e2d\u4ecebegin\u5f00\u59cb\u81f3end\u7ed3\u675f\u65f6\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9s;
split\u8bb2\u89e3\uff1a
java.lang.string.splitsplit \u65b9\u6cd5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8fd4\u56de\u3002stringObj.split([separator\uff0c[limit]])\u53c2\u6570stringObj \u5fc5\u9009\u9879\u3002\u8981\u88ab\u5206\u89e3\u7684 String \u5bf9\u8c61\u6216\u6587\u5b57\u3002\u8be5\u5bf9\u8c61\u4e0d\u4f1a\u88ab split \u65b9\u6cd5\u4fee\u6539\u3002separator \u53ef\u9009\u9879\u3002\u5b57\u7b26\u4e32\u6216 \u6b63\u5219\u8868\u8fbe\u5f0f \u5bf9\u8c61\uff0c\u5b83\u6807\u8bc6\u4e86\u5206\u9694\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u8fd8\u662f\u591a\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u5ffd
\u7565\u8be5\u9009\u9879\uff0c\u8fd4\u56de\u5305\u542b\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5355\u4e00\u5143\u7d20\u6570\u7ec4\u3002 limit\u53ef\u9009\u9879\u3002\u8be5\u503c\u7528\u6765\u9650\u5236\u8fd4\u56de\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002
\u8bf4\u660esplit \u65b9\u6cd5\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5728 stingObj \u4e2d\u6bcf\u4e2a\u51fa\u73b0 separator \u7684\u4f4d\u7f6e\u90fd\u8981\u8fdb\u884c\u5206\u89e3
\u3002separator \u4e0d\u4f5c\u4e3a\u4efb\u4f55\u6570\u7ec4\u5143\u7d20\u7684\u90e8\u5206\u8fd4\u56de\u3002
split \u7684\u5b9e\u73b0\u76f4\u63a5\u8c03\u7528\u7684 matcher \u7c7b\u7684 split \u7684\u65b9\u6cd5\u3002\u201c . \u201d\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u6709\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u7684\u65f6\u5019\u5fc5\u987b\u8fdb\u884c\u8f6c\u4e49\u3002
Java\u4ee3\u7801
public static void main(string[] args) {
string value = "192.168.128.33";
string[] names = value.split("\\.");
for (int i = 0; i < names.length; i++) {
system.out.println(names[i]);
}}
\u5982\u679c\u7528\u7ad6\u7ebf\u201c|\u201d\u5206\u9694\u7684\u8bdd\uff0c\u5c06\u51fa\u73b0\u4e0d\u53ef\u5f97\u5230\u7684\u7ed3\u679c\uff0c\u5fc5\u987b\u6539\u4e3a\u201c\\|\u201d
java\u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u7528string\u7c7b\u7684spilt\u65b9\u6cd5\uff0c\u9488\u5bf9\u67d0\u4e2a\u5206\u9694\u7b26\u6765\u5206\u5272\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a
public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4,5";//\u4e00\u4e2a\u5b57\u7b26\u4e32 String[] sourceStrArray = sourceStr.split(",");//\u5206\u5272\u51fa\u6765\u7684\u5b57\u7b26\u6570\u7ec4 for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } // \u6700\u591a\u5206\u5272\u51fa3\u4e2a\u5b57\u7b26\u4e32 int maxSplit = 3; sourceStrArray = sourceStr.split(",", maxSplit); for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } }}\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a
2
4
1
3,4,5
String 数组[] = str.split(某个字符);
注意:有些特殊字符比如/ . $不能直接使用
数组[]就自动将分割后的字符串存入了
按逗号分隔例子:
String str = "a,b,c";
String[] sth=str.split(","); //分隔后变成一个数组
split方法
toCharArray()
绛旓細java鍒嗗壊瀛楃涓茬敤split锛屼緥濡 String sourceStr = "1,2,3,4,5";String[] sourceStrArray = sourceStr.split(",");for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]);} split鍒嗛殧绗︽荤粨 1.瀛楃"|","*","+"閮藉緱鍔犱笂杞箟瀛楃锛屽墠闈㈠姞涓"...
绛旓細java涓垎闅斿瓧绗︿覆鐨勬柟寮忔湁涓ょ銆StringTokenizer 鍜 split銆 涓銆丼tringTokenizer锛//countTokens()鏂规硶 锛 //hasMoreTokens()鏂规硶 锛//nextToken()鏂规硶 String str = "tom,jack,lucy"; StringTokenizer commaToker = new StringTokenizer(str, ",");String[] result = new String[commaToker.countTok...
绛旓細Java涓殑split()鏂规硶鏄竴涓瓧绗︿覆绫籗tring鐨勬柟娉曪紝瀹冪殑浣滅敤鏄皢涓涓瓧绗︿覆鏍规嵁鎸囧畾鐨勫垎闅旂杩涜鍒嗗壊銆備緥濡傦紝鎴戜滑鏈変竴涓插瓧绗"apple,banana,orange"锛屽鏋滄垜浠互閫楀彿涓哄垎闅旂锛岄偅涔堜娇鐢╯plit()鏂规硶锛屽氨鍙互灏嗗瓧绗︿覆鍒嗗壊鎴愪笁涓瓙涓"apple"锛"banana"锛屽拰"orange"銆傝鏂规硶浣跨敤姝e垯琛ㄨ揪寮忎綔涓哄垎闅旂锛屽洜姝ゅ彲浠...
绛旓細鏍规嵁java鏍规嵁鎸囧畾鐨勫瓧绗︼紝鍒嗗壊瀛楃涓茬殑鏂规硶鏄細String temp[]=result.split(",");瀛楃涓插垎鍓--java涓璖tring.split()鐢ㄦ硶锛1銆佲.鈥濆拰鈥渱鈥濋兘鏄浆涔夊瓧绗︼紝蹇呴』寰楀姞"\\";濡傛灉鐢ㄢ.鈥濅綔涓哄垎闅旂殑璇濓紝蹇呴』鏄涓嬪啓娉曪細String.split("\\."),杩欐牱鎵嶈兘姝g‘鐨勫垎闅斿紑锛屼笉鑳界敤String.split(".");濡傛灉鐢...
绛旓細Java涓殑鎴戜滑鍙互鍒╃敤split鎶婂瓧绗︿覆鎸夌収鎸囧畾鐨勫垎鍓茬杩涜鍒嗗壊锛岃繑鍥炲瓧绗︿覆鏁扮粍锛宻tring.split鐨勭敤娉曞疄渚嬪強娉ㄦ剰浜嬮」锛歫ava.lang.string.split split 鏂规硶 灏嗕竴涓瓧绗︿覆鍒嗗壊涓哄瓙瀛楃涓诧紝鐒跺悗灏嗙粨鏋滀綔涓哄瓧绗︿覆鏁扮粍杩斿洖銆俿tringObj.split([separator锛孾limit]])stringObj 蹇呴夐」銆傝琚垎瑙g殑 String 瀵硅薄鎴栨枃瀛,...
绛旓細substring璁茶В锛歴锛漵.substring(int begin);鎴彇鎺塻浠庨瀛楁瘝璧烽暱搴︿负begin鐨勫瓧绗︿覆锛屽皢鍓╀綑瀛楃涓茶祴鍊肩粰s锛泂锛漵.substring(int begin锛宨nt end);鎴彇s涓粠begin寮濮嬭嚦end缁撴潫鏃鐨勫瓧绗︿覆锛屽苟灏嗗叾璧嬪肩粰s;split璁茶В锛java.lang.string.splitsplit 鏂规硶灏嗕竴涓瀛楃涓插垎鍓涓哄瓙瀛楃涓诧紝鐒跺悗灏嗙粨鏋滀綔涓哄瓧绗︿覆...
绛旓細java鎷嗗垎瀛楃涓浣跨敤string绫荤殑spilt鏂规硶锛岄拡瀵规煇涓垎闅旂鏉ュ垎鍓蹭竴涓瓧绗︿覆锛岀ず渚嬪涓嬶細public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4,5";//涓涓瓧绗︿覆 String[] sourceStrArray = sourceStr.split(",");//鍒嗗壊鍑烘潵鐨勫瓧绗鏁扮粍 for (int ...
绛旓細substring鏂规硶銆傚ぇ鑷存濊矾濡備笅锛歴tr4涓虹洰鏍囧瓧绗︿覆锛屽強鏈鍚庣殑缁撴灉浼氳瀛樻斁杩泂tr4 鎴戜滑鐢╯tringbuffer绫讳腑鐨勫嚱鏁癮ppend鏉ュ鐞嗗瓧绗︿覆鏁扮粍str1锛屽苟閫氳繃閫楀彿鏉鍒嗗壊锛屾渶鍚庣敤tostring鍑芥暟锛屽皢鍏惰浆鍙樻垚瀛楃涓茬殑褰㈠紡銆傛渶鍚庣敤substring鏉ュ缁撴灉杩涜鎴彇锛屽緱鍒版渶鍚庨渶瑕佺殑褰㈠紡锛歛bc锛123,45 涓嬪浘涓哄叿浣撶殑浠g爜娈碉細...
绛旓細瀛楃涓鍒囧垎鍙敤鍒皊plit鏂规硶锛涚ず渚嬩唬鐮侊細杈撳嚭缁撴灉锛氭敞鎰忎簨椤癸細1.瀛楃"|","*","+"閮藉緱鍔犱笂杞箟瀛楃锛屽墠闈㈠姞涓"\\"銆2.鑰屽鏋滄槸"\"锛岄偅涔堝氨寰楀啓鎴"\\\"銆3.濡傛灉涓涓瓧绗︿覆涓湁澶氫釜鍒嗛殧绗︼紝鍙互鐢"|"浣滀负杩炲瓧绗︺
绛旓細鎮ㄥソ锛JAVA涓瓧绗︿覆鎷嗗垎锛屼竴鑸娇鐢 split鏂规硶锛屽弬鏁版槸浣跨敤鎷嗗垎鐨勫瓧绗︿覆锛岃繑鍥炲兼槸 鎷嗗垎鍚庣殑缁撴灉鏁扮粍銆傛瘮濡傛偍鍐欑殑鍐呭锛屼娇鐢ㄦ柟娉曠殑鏂瑰紡涓猴細 String[] args = str.split("#\\+#"); 鍏朵腑+鍙蜂负鐗规畩瀛楃锛岄渶瑕佽浆涔夈傚叿浣撲唬鐮佸強杩愯缁撴灉濡備笅锛氭媶鍒嗛氳繃 split鏂规硶鍗冲彲瀹炵幇锛岄渶瑕佹敞鎰忔槸鍚﹀寘鍚壒娈婂瓧绗︾殑鎯呭喌銆