Regular expressions _2_ cutting the string

Expressions can be cut with a string of positive and have split (regex) method will cut the string, but writing a regular expression when the need to pay attention to the correct format, or after cutting to nothing.

package Regex;

public  class Played {


    public static void main(String[] args) {
        demo1();        
        
        demo2();
        
        demo3 ();
                
    }

    public static void demo3() {
        System.out.println ( "\\ is the string 'Oh correct wave ha...' Cutting results." );
        S2 String =. ".. Oh correct wave ha" ;
        String [] arr2=s2.split("\\.");
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }

    public  static  void demo2 () {
        System.out.println ( "string is the 'correct Oh ha wave...' Cutting results." );
        S1 String =. ".. Oh correct wave ha" ;
        String [] arr1=s1.split(".");
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
    }

    public  static  void demo1 () {
        System.out.println ( "The following is' cleavage results' strings' Oh ha correct wave apos" );
        S String = "Oh correct wave ha" ;
        String [] arr=s.split(" ");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}

 

 Use can be found. To cut the strings that result nothing, it is because representatives of any character, any character will match the cut, so in the end nothing, so if you want to use. Cutting you must use an escape character \\. so as to achieve the purpose. So if you want to have a regular repeating (and grammar), you need to use the escape character

 

 

Examples: There follows a string "9127463850" to write code to achieve the final output is: "2738465091"

package Regex;

import java.util.Arrays;

/*
 * Case 1
 * Requirements: following a string "9127463850" to write code to achieve the final output is: "2738465091"
 * Ideas:
 * An array of strings is cut into a string
 2 * int array into a string of equal length
 * 3 Sort results
 * The result makes up an array of strings 4
 * 
 * 
 */
public class SplitStringAndSort {


    public static void main(String[] args) {
        String s ="91 27 46 38 50";
        String [] ARR = s.split ( ""); // cutting string 
        int of arr1 [] = new new  int [arr.length]; // to be equal to an integer array space allocated 
        for ( int I = 0; I <ARR. length; I ++ ) {
            of arr1 [I] = the Integer.parseInt (ARR [I]); // the parseInt () method of shaping the string into digital numbers 
        }
        Arrays.sort (arr1); // Sort
        
        // string stitching, does not recommend the use of string method, a waste of space 
        System.out.println ( "The following is the string stitching ordinary method" );
        String str ="";
        for (int i = 0; i < arr1.length; i++) {
            if(i==arr1.length-1) {
                p = p + arr1 [i];
            }else
                str=str+arr1[i]+" ";
        }
        System.out.println(str);
        
        // use StringBuffer class, space is not wasted, and only a memory 
        System.out.println ( "The following is a method of the string class using the append StringBuffer () splice" );
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < arr1.length; i++) {
            if (i==arr1.length-1) {
                sb.append(arr1[i]);
                
            }
            else 
                sb.append(arr1[i]+" ");
        }
        System.out.println(sb);
    }


}

 

Can be found, as the result of the two methods, but the method a fee memory, ah method without fee memory. 

7

 

Guess you like

Origin www.cnblogs.com/tkg1314/p/11819395.html
Recommended