Here you can find the source of repeat(String item, int count)
item
string is repeated count
times.
Parameter | Description |
---|---|
item | string part to be repeated |
count | number of times to repeat <code>item</code> |
item
or empty string if item
is null
or empty or count
less than 1.
public static String repeat(String item, int count)
//package com.java2s; /*// w w w . j a va2 s . c om * The MIT License (MIT) * <p/> * Copyright (c) 2015 Maksym Dominichenko * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * <p/> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.util.*; public class Main { /** * Creates string where given <code>item</code> string is repeated <code>count</code> times. * * @param item * string part to be repeated * @param count * number of times to repeat <code>item</code> * * @return repeated <code>item</code> or empty string if <code>item</code> is <code>null</code> or empty or * <code>count</code> less than 1. */ public static String repeat(String item, int count) { StringBuilder result = new StringBuilder(); if (!isEmpty(item)) for (int i = 0; i < count; i++) result.append(item); return result.toString(); } /** * Helper to check if the String is {@code null} or empty.<br/> * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param string * A string to be checked * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise. */ public static boolean isEmpty(String string) { return string == null || string.length() == 0; } /** * Helper to check if the array is {@code null} or empty.<br/> * * @param array * An array to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Object[] array) { return array == null || array.length == 0; } /** * Helper to check if the collection is {@code null} or empty.<br/> * {@link Collection#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param collection * A collection to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } /** * Helper to check if the map is {@code null} or empty.<br/> * {@link Map#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param map * A map to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Map<?, ?> map) { return map == null || map.isEmpty(); } }