Java String Chop chopString(String argString, int argLength)

Here you can find the source of chopString(String argString, int argLength)

Description

Chops the string into smaller strings with size of argLength.

License

Apache License

Parameter

Parameter Description
argString a parameter
argLength a parameter

Declaration

public static String[] chopString(String argString, int argLength) 

Method Source Code

//package com.java2s;
/**//from  w  ww.  jav a2 s  . c om
 * Copyright 2010 Peter Brewer and Daniel Murphy
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Chops the string into smaller strings with size of
     * argLength. by daniel
     * 
     * @param argString
     * @param argLength
     * @return
     */
    public static String[] chopString(String argString, int argLength) {
        if (argString.length() == 0) {
            return new String[0];
        }
        if (argString.length() <= argLength) {
            return new String[] { argString };
        }

        String[] ret = new String[(int) Math.ceil(argString.length() / argLength)];
        int i;
        for (i = 0; i < ret.length - 1; i++) {
            ret[i] = argString.substring(i * argLength, (i + 1) * argLength);
        }
        ret[i] = argString.substring(i * argLength);
        // the last one, so we don't get the
        // out of bounds exception
        return ret;
    }
}

Related

  1. chopNull(String str)
  2. chopPrefix(String x, String prefix)
  3. ChopRt(String this_string, String chomp_off)
  4. chopScheme(String path)
  5. chopScheme(String path)
  6. chopString(String str, int len)
  7. chopString(String title, int maxLen)
  8. chopToNSignificantDigits(final float original, int no_of_sig_digits)
  9. chopTrailingChars(String source, char[] chars)