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

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

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

StringUtils.overlay介绍

[英]Overlays part of a String with another String.

A null string input returns null. A negative index is treated as zero. An index greater than the string length is treated as the string length. The start index is always the smaller of the two indices.

StringUtils.overlay(null, *, *, *)            = null 
StringUtils.overlay("", "abc", 0, 0)          = "abc" 
StringUtils.overlay("abcdef", null, 2, 4)     = "abef" 
StringUtils.overlay("abcdef", "", 2, 4)       = "abef" 
StringUtils.overlay("abcdef", "", 4, 2)       = "abef" 
StringUtils.overlay("abcdef", "zzzz", 2, 4)   = "abzzzzef" 
StringUtils.overlay("abcdef", "zzzz", 4, 2)   = "abzzzzef" 
StringUtils.overlay("abcdef", "zzzz", -1, 4)  = "zzzzef" 
StringUtils.overlay("abcdef", "zzzz", 2, 8)   = "abzzzz" 
StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" 
StringUtils.overlay("abcdef", "zzzz", 8, 10)  = "abcdefzzzz"

[中]用另一个字符串覆盖字符串的一部分。
空字符串输入返回空值。负指数被视为零。大于字符串长度的索引将被视为字符串长度。起始索引总是两个索引中较小的一个。

StringUtils.overlay(null, *, *, *)            = null 
StringUtils.overlay("", "abc", 0, 0)          = "abc" 
StringUtils.overlay("abcdef", null, 2, 4)     = "abef" 
StringUtils.overlay("abcdef", "", 2, 4)       = "abef" 
StringUtils.overlay("abcdef", "", 4, 2)       = "abef" 
StringUtils.overlay("abcdef", "zzzz", 2, 4)   = "abzzzzef" 
StringUtils.overlay("abcdef", "zzzz", 4, 2)   = "abzzzzef" 
StringUtils.overlay("abcdef", "zzzz", -1, 4)  = "zzzzef" 
StringUtils.overlay("abcdef", "zzzz", 2, 8)   = "abzzzz" 
StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" 
StringUtils.overlay("abcdef", "zzzz", 8, 10)  = "abcdefzzzz"

代码示例

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

@Test
public void testOverlay_StringStringIntInt() {
  assertNull(StringUtils.overlay(null, null, 2, 4));
  assertNull(StringUtils.overlay(null, null, -2, -4));
  assertEquals("", StringUtils.overlay("", null, 0, 0));
  assertEquals("", StringUtils.overlay("", "", 0, 0));
  assertEquals("zzzz", StringUtils.overlay("", "zzzz", 0, 0));
  assertEquals("zzzz", StringUtils.overlay("", "zzzz", 2, 4));
  assertEquals("zzzz", StringUtils.overlay("", "zzzz", -2, -4));
  assertEquals("abef", StringUtils.overlay("abcdef", null, 2, 4));
  assertEquals("abef", StringUtils.overlay("abcdef", null, 4, 2));
  assertEquals("abef", StringUtils.overlay("abcdef", "", 2, 4));
  assertEquals("abef", StringUtils.overlay("abcdef", "", 4, 2));
  assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 2, 4));
  assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 4, 2));
  assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", -1, 4));
  assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", 4, -1));
  assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -2, -1));
  assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -1, -2));
  assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 4, 10));
  assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 4));
  assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 8, 10));
  assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 8));
}

代码示例来源:origin: audit4j/audit4j-core

/**
 * Deidentify left.
 *
 * @param str the str
 * @param size the size
 * @return the string
 * 
 * @since 2.0.0
 */
public static String deidentifyLeft(String str, int size) {
  int repeat;
  if (size > str.length()) {
    repeat = str.length();
  } else {
    repeat = size;
  }
  return StringUtils.overlay(str, StringUtils.repeat('*', repeat), 0, size);
}

代码示例来源:origin: audit4j/audit4j-core

/**
 * Deidentify from left.
 *
 * @param str the str
 * @param size the size
 * @return the string
 * 
 * @since 2.0.0
 */
public static String deidentifyFromLeft(String str, int size) {
  int end = str.length();
  int repeat;
  if (size > str.length()) {
    repeat = 0;
  } else {
    repeat = str.length() - size;
  }
  return StringUtils.overlay(str, StringUtils.repeat('*', repeat), size, end);
}

代码示例来源:origin: audit4j/audit4j-core

/**
 * Deidentify from right.
 *
 * @param str the str
 * @param size the size
 * @return the string
 * 
 * @since 2.0.0
 */
public static String deidentifyFromRight(String str, int size) {
  int end = str.length();
  int repeat;
  if (size > str.length()) {
    repeat = str.length();
  } else {
    repeat = end - size;
  }
  return StringUtils.overlay(str, StringUtils.repeat('*', repeat), 0, end - size);
}

代码示例来源:origin: audit4j/audit4j-core

/**
 * Deidentify right.
 *
 * @param str the str
 * @param size the size
 * @return the string
 * 
 * @since 2.0.0
 */
public static String deidentifyRight(String str, int size) {
  int end = str.length();
  int repeat;
  if (size > str.length()) {
    repeat = str.length();
  } else {
    repeat = size;
  }
  return StringUtils.overlay(str, StringUtils.repeat('*', repeat), end - size, end);
}

代码示例来源:origin: audit4j/audit4j-core

/**
 * Deidentify middle.
 *
 * @param str the str
 * @param start the start
 * @param end the end
 * @return the string
 * 
 * @since 2.0.0
 */
public static String deidentifyMiddle(String str, int start, int end) {
  int repeat;
  if (end - start > str.length()) {
    repeat = str.length();
  } else {
    repeat = (str.length()- end) - start;
  }
  return StringUtils.overlay(str, StringUtils.repeat('*', repeat), start, str.length()-end);
}

相关文章

StringUtils类方法