Java join () character join function usage

Source: Internet
Author: User
Tags stringbuffer

The Join method is implemented through wait (note: The method provided by the Object ). When the main thread calls t. during the join operation, the main thread will get the t lock of the thread object (wait means to get the lock of the object), call the wait (wait time) of the object until the object wakes up the main thread, for example, after exiting.
This means that the main thread calls t. during the join operation, you must be able to get the lock of the thread t object. If you cannot get the lock, it cannot be wait. The just-opened example t. join (1000) does not indicate that the main thread waits for 1 second. If other threads get the t object lock before it waits, the waiting time is not 1 millisecond.

Instance

Import java. util. HashMap;
Import java. util. regex. Pattern;

/**
* Utilities for String formatting, manipulation, and queries.
* More information about this class is available from <a target = "_ top" href =
* "Http://ostermiller.org/utils/StringHelper.html"> ostermiller.org </a>.
 *
* @ Author Stephen Ostermiller http://ostermiller.org/contact.pl? Regarding = Java + Utilities
* @ Since ostermillerutils 1.00.00
*/
Public class StringHelper {
/**
* Join all the elements of a string array into a single
* String.
   *
* If the given array empty an empty string
* Will be returned. Null elements of the array are allowed
* And will be treated like empty Strings.
   *
* @ Param array Array to be joined into a string.
* @ Return Concatenation of all the elements of the given array.
* @ Throws NullPointerException if array is null.
   *
* @ Since ostermillerutils 1.05.00
*/
Public static String join (String [] array ){
Return join (array ,"");
  }

/**
* Join all the elements of a string array into a single
* String.
   *
* If the given array empty an empty string
* Will be returned. Null elements of the array are allowed
* And will be treated like empty Strings.
   *
* @ Param array Array to be joined into a string.
* @ Param delimiter String to place between array elements.
* @ Return Concatenation of all the elements of the given array with the delimiter in.
* @ Throws NullPointerException if array or delimiter is null.
   *
* @ Since ostermillerutils 1.05.00
*/
Public static String join (String [] array, String delimiter ){
// Cache the length of the delimiter
// Has the side effect of throwing a NullPointerException if
// The delimiter is null.
Int delimiterLength = delimiter. length ();

// Nothing in the array return empty string
// Has the side effect of throwing a NullPointerException if
// The array is null.
If (array. length = 0) return "";

// Only one thing in the array, return it.
If (array. length = 1 ){
If (array [0] = null) return "";
Return array [0];
    }

// Make a pass through and determine the size
// Of the resulting string.
Int length = 0;
For (int I = 0; I <array. length; I ++ ){
If (array [I]! = Null) length + = array [I]. length ();
If (I <array. length-1) length + = delimiterLength;
    }

// Make a second pass through and concatenate everything
// Into a string buffer.
StringBuffer result = new StringBuffer (length );
For (int I = 0; I <array. length; I ++ ){
If (array [I]! = Null) result. append (array [I]);
If (I <array. length-1) result. append (delimiter );
    }

Return result. toString ();
  }


}

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.