Requirement: divide "01: big car" into 01 and big car
There are two methods: substring
Java code
- PackageTest;
- Public ClassSubstringtest
- {
- Public Static VoidMain (string ARGs [])
- {
- String n = "01: Car ";
- String L = "";
- String r = "";
- IntK = n. Length ();
- For(IntI = 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: Car"; 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 );}}}
Another method was written by a person named la on csdn.
Package test
Java code
- Public ClassSplittest
- {
- Public Static VoidMain (string [] ARGs)
- {
- String S =NewString ("01: Car ");
- 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 to separate letters and numbers, simple regular gaps
Java code
- Public ClassTest01 {
- Public Static VoidMain (string [] ARGs ){
- String STR = "one123 ";
- String RegEx = "(? <= One )(? = 123 )";
- String [] STRs = Str. Split (RegEx );
- For(IntI = 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); truncate the string starting from the first letter of S and assign the remaining string to S;
S = S. substring (INT begin, int end); truncate the string from begin to end in S and assign it to S;
Split:
Java. Lang. String. Split
Split Method
Splits a string into substrings and returns the result as a string array.
Stringobj. Split ([separator, [limit])
Parameters
Stringobj
Required. String object or text to be decomposed. This object will not be modified by the split method.
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If
Returns a single array of elements that contain the entire string.
Limit
Optional. This value is used to limit the number of elements in the returned array.
Description
The result of the split method is a string array. In stingobj, the position where separator appears must be decomposed.
. The separator is not returned as part of any array element.
The split method of the matcher class directly called by the split implementation. "." Has a special meaning in the regular expression, so we must escape it when using it.
Java code
- Public Static VoidMain (string [] ARGs ){
- String value = "192.168.128.33 ";
- String [] names = value. Split ("\\.");
- For(IntI = 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]);}
If it is separated by a vertical line "|", the unattainable result will appear and must be changed to "\ |"