需求,把"01:大汽车",分成01和大汽车
有两种做法:一是substring
- package test;
- publicclass substringTest
- {
- publicstaticvoid main(String args[])
- {
- String N = "01:大汽车";
- String L="";
- String R="";
- int k= N.length();
- for (int i = 0; i < N.length(); i++)
- {
- if (N.substring(i, i + 1).equals("|"))
- {
- L=N.substring(0,i).trim();
- R=N.substring(i+1,k).trim();
- }
- else
- {
- }
- System.out.println(L);
- System.out.println(R);
- }
- }
- }
package test; public class substringTest { public static void main(String args[]) { String N = "01:大汽车"; String L=""; String R=""; int k= N.length(); for (int i = 0; i < N.length(); i++) { if (N.substring(i, i + 1).equals("|")) { L=N.substring(0,i).trim(); R=N.substring(i+1,k).trim(); } else { } System.out.println(L); System.out.println(R); } } }
另外一种方法是CSDN上一位叫老六的人给我写的
package Test
- publicclass splitTest
- {
- publicstaticvoid main(String[] args)
- {
- String s = new String("01:大汽车");
- String a[] = s.split(":");
- System.out.println(a[0]);
- System.out.println(a[1]);
- }
- }
public class splitTest { public static void main(String[] args) { String s = new String("01:大汽车"); String a[] = s.split(":"); System.out.println(a[0]); System.out.println(a[1]); } }
split分割字母和数字,简单正则缝隙
- publicclass Test01 {
- publicstaticvoid main(String[] args) {
- String str = "one123";
- String regex = "(?<=one)(?=123)";
- String[] strs = str.split(regex);
- for(int i = 0; i < strs.length; i++) {
- System.out.printf("strs[%d] = %s%n", i, strs[i]);
- }
- }
- }
public class Test01 { public static void main(String[] args) { String str = "one123"; String regex = "(?<=one)(?=123)"; String[] strs = str.split(regex); for(int i = 0; i < strs.length; i++) { System.out.printf("strs[%d] = %s%n", i, strs[i]); } } }
substring讲解:
s=s.substring(int begin);截取掉s从首字母起长度为begin的字符串,将剩余字符串赋值给s;
s=s.substring(int begin,int end);截取s中从begin开始至end结束时的字符串,并将其赋值给s;
split讲解:
java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回。 stringObj.split([separator,[limit]]) 参数 stringObj 必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。 separator 可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽
略该选项,返回包含整个字符串的单一元素数组。 limit 可选项。该值用来限制返回数组中的元素个数。
说明 split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解
。separator 不作为任何数组元素的部分返回。
split 的实现直接调用的 matcher 类的 split 的方法。“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义。
- publicstaticvoid 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]);
- }}
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]); }}
如果用竖线“|”分隔的话,将出现不可得到的结果,必须改为“\\|”
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)