Java join string connection usage

Source: Internet
Author: User
Tags join reset stringbuffer
1. You can use "+" to connect characters. 2. you can use the append method of stringBuffer to directly add characters to the end. you can use the above two methods to write the Join method that meets your requirements. import java. util. hashMap; import java. util. regex. pattern; public class StringHelper {public static String join (String [] array) {return join (array, "");} public static String join (String [] array, String delimiter) {int delimiterLength = delimiter. length (); if (array. length = 0) return ""; if (array. length = 1) {if (array [0] = null) return ""; return array [0];} // of the resulting string. int length = 0; for (int I = 0; INull. * @ Return Joined string. */public static String join (final StringBuffer buff, final Object array [], final String delim) {boolean haveDelim = (delim! = Null); for (int I = 0; I <array. length; I ++) {buff. append (array [I]); // if this is the last element then don't append delim if (haveDelim & (I + 1) <array. length) {buff. append (delim) ;}} return buff. toString ();}/*** Join an array of strings into one delimited string. ** @ param array Array of objects to join as strings. * @ param delim Delimiter to join strings with or Null. * @ Return Joined string. */public static String join (final Object array [], final String delim) {return join (new StringBuffer (), array, delim );} /*** Convert and join an array of objects into one string. ** @ param array Array of objects to join as strings. * @ return Converted and joined objects. */public static String join (final Object array []) {return join (array, null);}/*** Convert and join an array of bytes into one string. ** @ param array Array of objects to join as strings. * @ return Converted and joined objects. */public static String join (final byte array []) {Byte bytes [] = new Byte [array. length]; for (int I = 0; I <bytes. length; I ++) {bytes [I] = new Byte (array [I]);} return join (bytes, null );} /*** Return a string composed of the given array. ** @ param buff Buffer used to construct string value (not reset ). * @ param array Array of objects. * @ param prefix String prefix. * @ param separator Element sepearator. * @ param suffix String suffix. * @ return String in the format of: * prefix + n (+ separator + n + I) * + suffix. */public static String join (final StringBuffer buff, final Object [] array, final String prefix, final String separator, final String suffix) {buff. append (prefix); join (buff, array, separator); buff. append (suffix); return buff. toString ();}/*** Return a string composed of the given array. ** @ param array Array of objects. * @ param prefix String prefix. * @ param separator Element sepearator. * @ param suffix String suffix. * @ return String in the format of: * prefix + n (+ separator + n + I) * + suffix. */public static String join (final Object [] array, final String prefix, final String separator, final String suffix) {return join (new StringBuffer (), array, prefix, separator, suffix );}}

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.