org.apache.commons.lang3.StringUtils.repeat()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(129)

本文整理了Java中org.apache.commons.lang3.StringUtils.repeat()方法的一些代码示例,展示了StringUtils.repeat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringUtils.repeat()方法的具体详情如下:
包路径:org.apache.commons.lang3.StringUtils
类名称:StringUtils
方法名:repeat

StringUtils.repeat介绍

[英]Returns padding using the specified delimiter repeated to a given length.

StringUtils.repeat('e', 0)  = "" 
StringUtils.repeat('e', 3)  = "eee" 
StringUtils.repeat('e', -2) = ""

Note: this method does not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using #repeat(String,int) instead.
[中]返回使用指定分隔符重复指定长度的填充。

StringUtils.repeat('e', 0)  = "" 
StringUtils.repeat('e', 3)  = "eee" 
StringUtils.repeat('e', -2) = ""

注意:此方法不支持使用Unicode Supplementary Characters填充,因为它们需要表示一对字符。如果需要支持应用程序的完整I18N,请考虑使用“重复”(String,int)。

代码示例

代码示例来源:origin: org.apache.commons/commons-lang3

/**
   * Represents this token as a String.
   *
   * @return String representation of the token
   */
  @Override
  public String toString() {
    return StringUtils.repeat(this.value.toString(), this.count);
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Repeat a String {@code repeat} times to form a
 * new String, with a String separator injected each time. </p>
 *
 * <pre>
 * StringUtils.repeat(null, null, 2) = null
 * StringUtils.repeat(null, "x", 2)  = null
 * StringUtils.repeat("", null, 0)   = ""
 * StringUtils.repeat("", "", 2)     = ""
 * StringUtils.repeat("", "x", 3)    = "xxx"
 * StringUtils.repeat("?", ", ", 3)  = "?, ?, ?"
 * </pre>
 *
 * @param str        the String to repeat, may be null
 * @param separator  the String to inject, may be null
 * @param repeat     number of times to repeat str, negative treated as zero
 * @return a new String consisting of the original String repeated,
 *  {@code null} if null String input
 * @since 2.5
 */
public static String repeat(final String str, final String separator, final int repeat) {
  if(str == null || separator == null) {
    return repeat(str, repeat);
  }
  // given that repeat(String, int) is quite optimized, better to rely on it than try and splice this into it
  final String result = repeat(str + separator, repeat);
  return removeEnd(result, separator);
}

代码示例来源:origin: apache/incubator-pinot

private String getStringForDisplay(String memoryStr) {
  int numSpacesToPad = MEMORY_STR_LEN - memoryStr.length();
  return memoryStr + StringUtils.repeat(" ", numSpacesToPad);
 }
}

代码示例来源:origin: pmd/pmd

private static StringBuilder newLine(StringBuilder builder, int indentDepth) {
    return builder.append("\n").append(StringUtils.repeat(' ', indentDepth));
  }
}

代码示例来源:origin: neo4j/neo4j

private static String whitespace( int numberOfWhitespaces )
  {
    return StringUtils.repeat( " ", numberOfWhitespaces );
  }
}

代码示例来源:origin: apache/hbase

/**
  * Push the input string to the right by appending a character before it, usually a space.
  * @param input the string to pad
  * @param padding the character to repeat to the left of the input string
  * @param length the desired total length including the padding
  * @return padding characters + input
  */
 public static String padFront(String input, char padding, int length) {
  if (input.length() > length) {
   throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length);
  }
  int numPaddingCharacters = length - input.length();
  return StringUtils.repeat(padding, numPaddingCharacters) + input;
 }
}

代码示例来源:origin: apache/flink

private static String getGenericTypeTree(Class<?> type, int indent) {
  String ret = "";
  for (Field field : type.getDeclaredFields()) {
    if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
      continue;
    }
    ret += StringUtils.repeat(' ', indent) + field.getName() + ":" + field.getType().getName() +
      (field.getType().isEnum() ? " (is enum)" : "") + "\n";
    if (!field.getType().isPrimitive()) {
      ret += getGenericTypeTree(field.getType(), indent + 4);
    }
  }
  return ret;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

@Override
public String getCommandUsage() {
 StringBuffer sbuf = new StringBuffer(USAGE_PREFIX + COMMANDS);
 String banner = StringUtils.repeat("=", 66);
 sbuf.append(banner + "\n");
 sbuf.append(CreateCommand.USAGE + ":\n\n" + CreateCommand.DESC + "\n");
 sbuf.append(banner + "\n");
 sbuf.append(DeleteCommand.USAGE + ":\n\n" + DeleteCommand.DESC + "\n");
 sbuf.append(banner + "\n");
 sbuf.append(ListCommand.USAGE + ":\n\n" + ListCommand.DESC + "\n");
 return sbuf.toString();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

protected String toString(int tabs) {
  StringBuilder sb = new StringBuilder();
  if (MapUtils.isNotEmpty(map)) {
    sb.append("{\n");
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      sb.append(getString(entry.getKey(), entry.getValue(), tabs+1));
    }
    sb.append(StringUtils.repeat(" ", (tabs-1)*2)).append("}");
  }
  return sb.toString();
}

代码示例来源:origin: apache/flink

private static <T> String getSerializerTree(TypeInformation<T> ti, int indent) {
  String ret = "";
  if (ti instanceof CompositeType) {
    ret += StringUtils.repeat(' ', indent) + ti.getClass().getSimpleName() + "\n";
    CompositeType<T> cti = (CompositeType<T>) ti;
    String[] fieldNames = cti.getFieldNames();
    for (int i = 0; i < cti.getArity(); i++) {
      TypeInformation<?> fieldType = cti.getTypeAt(i);
      ret += StringUtils.repeat(' ', indent + 2) + fieldNames[i] + ":" + getSerializerTree(fieldType, indent);
    }
  } else {
    if (ti instanceof GenericTypeInfo) {
      ret += StringUtils.repeat(' ', indent) + "GenericTypeInfo (" + ti.getTypeClass().getSimpleName() + ")\n";
      ret += getGenericTypeTree(ti.getTypeClass(), indent + 4);
    } else {
      ret += StringUtils.repeat(' ', indent) + ti.toString() + "\n";
    }
  }
  return ret;
}

代码示例来源:origin: apache/nifi

private static void printBlankKeyWarning() {
  logger.error(StringUtils.repeat("*", 80));
  logger.error(centerString("A blank sensitive properties key was provided"));
  logger.error(centerString("Specify a unique key in nifi.properties"));
  logger.error(centerString("for nifi.sensitive.props.key"));
  logger.error(centerString(""));
  logger.error(centerString("The Encrypt Config Tool in NiFi Toolkit can be used to"));
  logger.error(centerString("migrate the flow to the new key"));
  logger.error(StringUtils.repeat("*", 80));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testRepeat_StringStringInt() {
  assertNull(StringUtils.repeat(null, null, 2));
  assertNull(StringUtils.repeat(null, "x", 2));
  assertEquals("", StringUtils.repeat("", null, 2));
  assertEquals("", StringUtils.repeat("ab", "", 0));
  assertEquals("", StringUtils.repeat("", "", 2));
  assertEquals("xx", StringUtils.repeat("", "x", 3));
  assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testRepeat_CharInt() {
  assertEquals("zzz", StringUtils.repeat('z', 3));
  assertEquals("", StringUtils.repeat('z', 0));
  assertEquals("", StringUtils.repeat('z', -2));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testRepeat_StringInt() {
  assertNull(StringUtils.repeat(null, 2));
  assertEquals("", StringUtils.repeat("ab", 0));
  assertEquals("", StringUtils.repeat("", 3));
  assertEquals("aaa", StringUtils.repeat("a", 3));
  assertEquals("", StringUtils.repeat("a", -2));
  assertEquals("ababab", StringUtils.repeat("ab", 3));
  assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
  final String str = StringUtils.repeat("a", 10000);  // bigger than pad limit
  assertEquals(10000, str.length());
  assertTrue(StringUtils.containsOnly(str, 'a'));
}

代码示例来源:origin: alibaba/jvm-sandbox

private void progress(int index) {
  if (printer.isBroken()) {
    return;
  }
  final int rate = computeRate(index);
  printer.print(String.format("\r%s[%-" + width + "s]", prefix, StringUtils.repeat('#', rate)));
  printer.flush();
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldTrimDownLogFileNameToAReasonableSizeIfThePluginIdIsTooBig() throws Exception {
  assertPluginLogFile("abcd", "plugin-abcd.log");
  String pluginIdWithLengthOf189 = repeat("a", 189);
  assertPluginLogFile(pluginIdWithLengthOf189, "plugin-" + pluginIdWithLengthOf189 + ".log");
  String pluginIdWithLengthOf190 = repeat("a", 190);
  assertPluginLogFile(pluginIdWithLengthOf190, "plugin-" + pluginIdWithLengthOf189 + ".log");
  String pluginIdWithLengthOf200 = repeat("a", 200);
  assertPluginLogFile(pluginIdWithLengthOf200, "plugin-" + pluginIdWithLengthOf189 + ".log");
}

代码示例来源:origin: ata4/disunity

private void printTypeNodeText(Node<? extends Type> node, int level) {
  String indent = StringUtils.repeat("  ", level);
  Type type = node.data();
  output().printf("% 4d: %s%s %s (metaFlag: %x)%n", type.index(), indent,
      type.typeName(), type.fieldName(), type.metaFlag());
  node.forEach(t -> printTypeNodeText(t, level + 1));
}

代码示例来源:origin: apache/kylin

@Test
  public void testOverflow() {
    String text = StringUtils.repeat("h", 21);
    ExtendedColumnSerializer serializer = new ExtendedColumnSerializer(DataType.getType("extendedcolumn(20)"));
    MeasureIngester<ByteArray> ingester = measureType.newIngester();
    ByteArray array = ingester.valueOf(new String[] { null, text }, null, null);

    ByteBuffer buffer = ByteBuffer.allocate(serializer.maxLength());
    serializer.serialize(array, buffer);
    buffer.flip();
    ByteArray des = serializer.deserialize(buffer);
    Assert.assertTrue(new ByteArray(StringUtils.repeat("h", 20).getBytes(StandardCharsets.UTF_8)).equals(des));
  }
}

代码示例来源:origin: gocd/gocd

@Test(timeout = 1000)
public void shouldValidateLeadingAndTrailingSpacesOnExecCommandInReasonableTime() throws Exception {
  // See https://github.com/gocd/gocd/issues/3551
  // This is only reproducible on longish strings, so don't try shortening the exec task length...
  String longPath = StringUtils.repeat("f", 100);
  CruiseConfig config = GoConfigMother.configWithPipelines("pipeline1");
  config.findJob("pipeline1", "stage", "job").addTask(new ExecTask(longPath + " ", "arg1", (String) null));
  output = new ByteArrayOutputStream();
  try {
    xmlWriter.write(config, output, false);
    fail("expected to blow up");
  } catch (XsdValidationException e) {
    assertThat(e.getMessage(), containsString("should conform to the pattern - \\S(.*\\S)?"));
  }
}

代码示例来源:origin: apache/kylin

@Test
public void testNormal() {
  String text = StringUtils.repeat("h", 20);
  ExtendedColumnSerializer serializer = new ExtendedColumnSerializer(DataType.getType("extendedcolumn(20)"));
  MeasureIngester<ByteArray> ingester = measureType.newIngester();
  ByteArray array = ingester.valueOf(new String[] { null, text }, null, null);
  ByteBuffer buffer = ByteBuffer.allocate(serializer.maxLength());
  serializer.serialize(array, buffer);
  buffer.flip();
  ByteArray des = serializer.deserialize(buffer);
  Assert.assertTrue(new ByteArray(text.getBytes(StandardCharsets.UTF_8)).equals(des));
}

相关文章

StringUtils类方法