Java intercepts string, String, substring, and split, and splits letters and numbers into regular gaps.

Source: Internet
Author: User

Requirement: divide "01: big car" into 01 and big car

There are two methods: substring

Java code
  1. PackageTest;
  2. Public ClassSubstringtest
  3. {
  4.  Public Static VoidMain (string ARGs [])
  5. {
  6. String n = "01: Car ";
  7. String L = "";
  8. String r = "";
  9. IntK = n. Length ();
  10. For(IntI = 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. System. Out. println (L );
  21. System. Out. println (R );
  22. }
  23. }
  24. }
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
  1. Public ClassSplittest
  2. {
  3. Public Static VoidMain (string [] ARGs)
  4. {
  5. String S =NewString ("01: Car ");
  6. String a [] = S. Split (":");
  7. System. Out. println (A [0]);
  8. System. Out. println (A [1]);
  9. }
  10. }
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
  1. Public ClassTest01 {
  2. Public Static VoidMain (string [] ARGs ){
  3. String STR = "one123 ";
  4. String RegEx = "(? <= One )(? = 123 )";
  5. String [] STRs = Str. Split (RegEx );
  6. For(IntI = 0; I <STRs. length; I ++ ){
  7. System. Out. printf ("STRs [% d] = % S % N", I, STRs [I]);
  8. }
  9. }
  10. }
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
  1. Public Static VoidMain (string [] ARGs ){
  2. String value = "192.168.128.33 ";
  3. String [] names = value. Split ("\\.");
  4. For(IntI = 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]);}

If it is separated by a vertical line "|", the unattainable result will appear and must be changed to "\ |"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.