Java about split split string, empty string cannot get the problem

Source: Internet
Author: User
Java about split split string, empty string cannot get the problem
    1. Class T {
    2. public static void Main (String args[]) {
    3. String num[] = new STRING[11];
    4. String sline = "101494|360103660318444|2008/06/17| Zhou Runying |1292.0|3085.76|2778.28|912.91|106.0| | |";
    5. num = Sline.split ("\\|");
    6. int row = 1;
    7. for (String s:num) {
    8. System.out.println (row+++ "=" +s);
    9. }
    10. }
    11. }
There are three of the most | | |, running results
1=101494
2=360103660318444
3=2008/06/17
4= Zhou Runying
5=1292.0
6=3085.76
7=2778.28
8=912.91
9=106.0


To view the API, there is a
    1. Public string[] Split (String regex, int limit);


The limit parameter controls the number of times the pattern is applied, thus affecting the length of the resulting array

if the limit n is greater than 0, then the pattern applies n>-1 times, the length of the array is not greater than N, and the last entry of the array will contain all inputs except the last match delimiter

if n non-positive, the number of times the pattern is applied is unlimited and the array can be any length

if n is zero, the number of times the pattern is applied is unlimited, and the array can be any length,and discards the trailing empty string

Modify the code to
    1. Class T {
    2. public static void Main (String args[]) {
    3. String num[] = new STRING[11];
    4. String sline = "101494|360103660318444|2008/06/17| Zhou Runying |1292.0|3085.76|2778.28|912.91|106.0| | |";
    5. num = Sline.split ("\\|",-1); Use 1 as a parameter here
    6. int row = 1;
    7. for (String s:num) {
    8. System.out.println (row+++ "=" +s);
    9. }
    10. }
    11. }

Run the result as
1=101494
2=360103660318444
3=2008/06/17
4= Zhou Runying
5=1292.0
6=3085.76
7=2778.28
8=912.91
9=106.0
10=
11=
12=


Results are normal.

Java about split split string, empty string cannot get the problem

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.