Computer Question (intermediate)-re-split the strings in the array by the specified length (Java)

Source: Internet
Author: User

Computer Question (intermediate)-re-split the strings in the array by the specified length (Java)

The questions are as follows:

The Code is as follows:

Package huawei; import java. util. arrayList; public final class Demo {/** function: compile a function and input it as a string array. * split each string in the array according to the specified length iInputLenth, output to the new string array. If the length of a string is not an integer multiple of iInputLenth, add 0 to the end. * Null strings are not processed. If an empty string is encountered, the array ends and the function returns the result. Input: String inputStrArray String Array pointer * the maximum number of strings is 50, and the maximum length of a String is 255. The iInputLenth Length Specified by int iInputLenth is iInputLenth >=1 & <= 32 **. Return: string Array pointer ** example split by length 8 Input: abc 12345789 return: abc00000 12345678 90000000 */public static String [] convertStringArray (String [] inputStrArray, int iInputLenth) {if (iInputLenth> = 1 & iInputLenth <= 32) // split length {ArrayList
 
  
ArrayList = new ArrayList
  
   
(); For (int I = 0; I <inputStrArray. length; I ++) {String srcString = inputStrArray [I]; if (srcString. length ()> 0) // not empty {char src [] = srcString. toCharArray (); // converts a string to an array of characters int len = src. length; // String Array length StringBuilder sb = new StringBuilder (); boolean isContinue = true; int ring = 1; // used to clear the count for (int index = 0; isContinue; index ++, ring ++) {if (index = (len-1) // The length and Index are both {sb. append (src [index]); int tmp_len = sb. length (); for (; tmp_len <iInputLenth; tmp_len ++) {sb. append ("0"); // Add zero} arrayList. add (sb. toString (); isContinue = false; // do not continue} else {sb. append (src [index]); // convert the character array to if (ring = iInputLenth) // if the length is equal to the split length, split it once and save sb to the list, and clear sb, the next round starts {ring = 0; arrayList. add (sb. toString (); sb = new StringBuilder () ;}}} if (arrayList. size ()> 0) {String data [] = new String [arrayList. size ()]; data = arrayList. toArray (data); // convert the List into a string array return data ;}} return null ;}}
  
 

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.