如何截取字符串

需求,把"01:大汽车",分成01和大汽车

有两种做法:一是substring

Java代码 复制代码 收藏代码
  1. package test; 
  2.  
  3. publicclass substringTest 
  4. publicstaticvoid main(String args[])  
  5. {  
  6.   String N = "01:大汽车";  
  7.   String L="";  
  8.   String R="";  
  9.   int k= N.length();  
  10.   for (int i = 0; i < N.length(); i++)  
  11.   {  
  12.    if (N.substring(i, i + 1).equals("|"))  
  13.    {    
  14.     L=N.substring(0,i).trim();  
  15.     R=N.substring(i+1,k).trim();  
  16.    }  
  17.    else  
  18.    {  
  19.                
  20.    }  
  21.    System.out.println(L);  
  22.    System.out.println(R);  
  23.   } 
  24. }  
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

Java代码 复制代码 收藏代码
  1. publicclass splitTest 
  2.     publicstaticvoid main(String[] args) 
  3.     { 
  4.         String s = new String("01:大汽车");  
  5.         String a[] = s.split(":"); 
  6.      
  7.         System.out.println(a[0]); 
  8.         System.out.println(a[1]); 
  9.     } 
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分割字母和数字,简单正则缝隙

Java代码 复制代码 收藏代码
  1. publicclass Test01 { 
  2.     publicstaticvoid main(String[] args) { 
  3.         String str = "one123"
  4.         String regex = "(?<=one)(?=123)"
  5.         String[] strs = str.split(regex); 
  6.         for(int i = 0; i < strs.length; i++) { 
  7.             System.out.printf("strs[%d] = %s%n", i, strs[i]); 
  8.         } 
  9.     } 
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 的方法。“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义。

Java代码 复制代码 收藏代码
  1. publicstaticvoid main(string[] args) { 
  2. string value = "192.168.128.33"
  3. string[] names = value.split("\\."); 
  4. for (int i = 0; i < names.length; i++) { 
  5. system.out.println(names[i]); 
  6. }} 
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]);
}}

如果用竖线“|”分隔的话,将出现不可得到的结果,必须改为“\\|” 

黑色头发  http://heisetoufa.iteye.com

原文地址:https://www.cnblogs.com/guanghuiqq/p/2680113.html