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

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

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

StringUtils.replaceEach介绍

[英]Replaces all occurrences of Strings within another String.

A null reference passed to this method is a no-op, or if any "search string" or "string to replace" is null, that replace will be ignored. This will not repeat. For repeating replaces, call the overloaded method.

StringUtils.replaceEach(null, *, *)        = null 
StringUtils.replaceEach("", *, *)          = "" 
StringUtils.replaceEach("aba", null, null) = "aba" 
StringUtils.replaceEach("aba", new String[0], null) = "aba" 
StringUtils.replaceEach("aba", null, new String[0]) = "aba" 
StringUtils.replaceEach("aba", new String[]{"a"}, null)  = "aba" 
StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""})  = "b" 
StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"})  = "aba" 
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"})  = "wcte" 
(example of how it does not repeat) 
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})  = "dcte"

[中]替换另一个字符串中出现的所有字符串。
传递给此方法的null引用为no op,或者如果任何“搜索字符串”或“要替换的字符串”为null,则将忽略该替换。这不会重复。对于重复替换,请调用重载方法。

StringUtils.replaceEach(null, *, *)        = null 
StringUtils.replaceEach("", *, *)          = "" 
StringUtils.replaceEach("aba", null, null) = "aba" 
StringUtils.replaceEach("aba", new String[0], null) = "aba" 
StringUtils.replaceEach("aba", null, new String[0]) = "aba" 
StringUtils.replaceEach("aba", new String[]{"a"}, null)  = "aba" 
StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""})  = "b" 
StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"})  = "aba" 
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"})  = "wcte" 
(example of how it does not repeat) 
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})  = "dcte"

代码示例

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

return replaceEach(text, searchList, replacementList, false, 0);

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

return replaceEach(text, searchList, replacementList, true, timeToLive);

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

/**
 * Resolve {@value #DATABASE_TOKEN} and {@value #TABLE_TOKEN} in <code>rawString</code> to {@link Table#getDbName()}
 * and {@link Table#getTableName()}
 */
public static String resolveTemplate(String rawString, Table table) {
 if (StringUtils.isBlank(rawString)) {
  return rawString;
 }
 return StringUtils.replaceEach(rawString, new String[] { DATABASE_TOKEN, TABLE_TOKEN }, new String[] { table.getDbName(), table.getTableName() });
}

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

return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1);

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

public void testReplace_StringStringArrayStringArray() {
  assertNull(StringUtils.replaceEach(null, new String[]{"a"}, new String[]{"b"}));
  assertEquals(StringUtils.replaceEach("", new String[]{"a"}, new String[]{"b"}), "");
  assertEquals(StringUtils.replaceEach("aba", null, null), "aba");
  assertEquals(StringUtils.replaceEach("aba", new String[0], null), "aba");
  assertEquals(StringUtils.replaceEach("aba", null, new String[0]), "aba");
  assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, null), "aba");
  assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""}), "b");
  assertEquals(StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}), "aba");
  assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}), "wcte");
  assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}), "dcte");
  assertEquals("bcc", StringUtils.replaceEach("abc", new String[]{"a", "b"}, new String[]{"b", "c"}));
  assertEquals("q651.506bera", StringUtils.replaceEach("d216.102oren",
      new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
          "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
  assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{null}), "aba");
  assertEquals(StringUtils.replaceEach("aba", new String[]{"a", "b"}, new String[]{"c", null}), "cbc");
    StringUtils.replaceEach("abba", new String[]{"a"}, new String[]{"b", "a"});
    fail("StringUtils.replaceEach(String, String[], String[]) expecting IllegalArgumentException");
  } catch (final IllegalArgumentException ex) {

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

List<String> resolvedValueList = Lists.newArrayList();
for (String rawValue : rawValueList) {
 String resolvedValue = StringUtils.replaceEach(rawValue,
   new String[] { DATABASE_TOKEN, TABLE_TOKEN, LOGICAL_DB_TOKEN, LOGICAL_TABLE_TOKEN },
   new String[] { realDbAndTable.getDb(), realDbAndTable.getTable(), logicalDbAndTable.getDb(), logicalDbAndTable.getTable() });
String resolvedValue = StringUtils.replaceEach(resolvedConfig.getString(entry.getKey()),
 new String[] { DATABASE_TOKEN, TABLE_TOKEN, LOGICAL_DB_TOKEN, LOGICAL_TABLE_TOKEN },
 new String[] { realDbAndTable.getDb(), realDbAndTable.getTable(), logicalDbAndTable.getDb(), logicalDbAndTable.getTable() });

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

private String applyFunctionForCommonOperator(String whereClause, String columnName, boolean
    isDate) {
  String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN :
      PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN;
  String pattern = StringUtils.replaceEach(PhoenixStorageHandlerConstants
          .COMMON_OPERATOR_PATTERN,
      new String[]{PhoenixStorageHandlerConstants.COLUMNE_MARKER,
          PhoenixStorageHandlerConstants.PATERN_MARKER}, new String[]{columnName,
          targetPattern});
  Matcher matcher = Pattern.compile(pattern).matcher(whereClause);
  while (matcher.find()) {
    String token = matcher.group(1);
    String datePart = matcher.group(3);
    String convertString = token.replace(datePart, applyFunction(isDate ?
        PhoenixStorageHandlerConstants.DATE_FUNCTION_TEMPLETE :
        PhoenixStorageHandlerConstants.TIMESTAMP_FUNCTION_TEMPLATE, datePart));
    whereClause = whereClause.replaceAll(StringUtils.replaceEach(token, new String[]{"(",
        ")"}, new String[]{"\\(", "\\)"}), convertString);
  }
  return whereClause;
}

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

/**
 * Escapes all sql wildcards in the given string, returns the same string if it doesn't contain
 * any sql wildcards
 *
 * @param oldString the string in which to escape the sql wildcards
 * @param connection The underlying database connection
 * @return the string with sql wildcards escaped if any found otherwise the original string is
 *         returned
 */
public static String escapeSqlWildcards(String oldString, Connection connection) {
  
  //replace all sql wildcards if any
  if (!StringUtils.isBlank(oldString)) {
    String escapeCharacter = "";
    
    try {
      //get the database specific escape character from the metadata
      escapeCharacter = connection.getMetaData().getSearchStringEscape();
    }
    catch (SQLException e) {
      log.warn("Error generated", e);
    }
    //insert an escape character before each sql wildcard in the search phrase
    return StringUtils.replaceEach(oldString, new String[] { "%", "_", "*", "'" }, new String[] {
        escapeCharacter + "%", escapeCharacter + "_", escapeCharacter + "*", "''" });
  } else {
    return oldString;
  }
}

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

private String applyFunctionForInOperator(String whereClause, String columnName, boolean
    isDate) {
  String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN :
      PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN;
  String pattern = StringUtils.replaceEach(PhoenixStorageHandlerConstants.IN_OPERATOR_PATTERN,
      new String[]{PhoenixStorageHandlerConstants.COLUMNE_MARKER,
          PhoenixStorageHandlerConstants.PATERN_MARKER}, new String[]{columnName,
          targetPattern});
  String itemPattern = "(" + targetPattern + ")";
  Matcher matcher = Pattern.compile(pattern).matcher(whereClause);
  while (matcher.find()) {
    String token = matcher.group(1);
    Matcher itemMatcher = Pattern.compile(itemPattern).matcher(token);
    while (itemMatcher.find()) {
      String item = itemMatcher.group(1);
      token = token.replace(item, applyFunction(isDate ? PhoenixStorageHandlerConstants
          .DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants
          .TIMESTAMP_FUNCTION_TEMPLATE, item));
    }
    whereClause = whereClause.replaceAll(pattern, token);
  }
  return whereClause;
}

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

private String applyFunctionForBetweenOperator(String whereClause, String columnName, boolean
    isDate) {
  String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN :
      PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN;
  String pattern = StringUtils.replaceEach(PhoenixStorageHandlerConstants
          .BETWEEN_OPERATOR_PATTERN,
      new String[]{PhoenixStorageHandlerConstants.COLUMNE_MARKER,
          PhoenixStorageHandlerConstants.PATERN_MARKER}, new String[]{columnName,
          targetPattern});
  Matcher matcher = Pattern.compile(pattern).matcher(whereClause);
  while (matcher.find()) {
    String token = matcher.group(1);
    boolean isNot = matcher.group(2) == null ? false : true;
    String fromDate = matcher.group(3);
    String toDate = matcher.group(4);
    String convertString = StringUtils.replaceEach(token, new String[]{fromDate, toDate},
        new String[]{applyFunction(isDate ? PhoenixStorageHandlerConstants
            .DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants
            .TIMESTAMP_FUNCTION_TEMPLATE, fromDate),
            applyFunction(isDate ? PhoenixStorageHandlerConstants
                .DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants
                .TIMESTAMP_FUNCTION_TEMPLATE, toDate)});
    whereClause = whereClause.replaceAll(pattern, convertString);
  }
  return whereClause;
}

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

private List<String> buildWhereClause(JobConf jobConf, StringBuilder sql, String whereClause,
                   Map<String, TypeInfo> columnTypeMap) throws IOException {
  if (whereClause == null || whereClause.isEmpty()) {
    return Collections.emptyList();
  }
  List<String> conditionColumnList = Lists.newArrayList();
  sql.append(" where ");
  whereClause = StringUtils.replaceEach(whereClause, new String[]{"UDFToString"}, new
      String[]{"to_char"});
  for (String columnName : columnTypeMap.keySet()) {
    if (whereClause.contains(columnName)) {
      String column = findReplacement(jobConf, columnName);
      whereClause = whereClause.replaceAll("\\b" + columnName + "\\b", "\"" + column + "\"");
      conditionColumnList.add(column);
      if (PhoenixStorageHandlerConstants.DATE_TYPE.equals(
          columnTypeMap.get(columnName).getTypeName())) {
        whereClause = applyDateFunctionUsingRegex(whereClause, column);
      } else if (PhoenixStorageHandlerConstants.TIMESTAMP_TYPE.equals(
          columnTypeMap.get(columnName).getTypeName())) {
        whereClause = applyTimestampFunctionUsingRegex(whereClause, column);
      }
    }
  }
  sql.append(whereClause);
  return conditionColumnList;
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
 * Sanitize a string for use in Matcher.appendReplacement.
 * Replaces all \ with \\ and $ as \$ because they are used as control
 * characters in appendReplacement.
 *
 * @param toSanitize the string to sanitize
 * @return sanitized version of the given string
 */
public static String sanitizeForAppendReplacement(final String toSanitize) {
  return org.apache.commons.lang3.StringUtils.replaceEach(toSanitize,
                new String[] {"\\", "$"}, new String[]{"\\\\", "\\$"});
}

代码示例来源:origin: de.vandermeer/char-translation

@Override
  public String translateHtmlElements(String input){
    return StringUtils.replaceEach(input, this.searchListHE, this.latexListHE);
  }
}

代码示例来源:origin: org.xwiki.rendering/xwiki-rendering-syntax-xwiki2

/**
 * @param text the reference to which to add escapes to
 * @return the modified text
 */
protected String addEscapesToReferencePart(String text)
{
  return StringUtils.replaceEach(text, ESCAPES_REFERENCE, ESCAPE_REPLACEMENTS_REFERENCE);
}

代码示例来源:origin: org.xwiki.rendering/xwiki-rendering-syntax-xwiki20

/**
 * @param text the reference to which to add escapes to
 * @return the modified text
 */
protected String addEscapesToReferencePart(String text)
{
  return StringUtils.replaceEach(text, ESCAPES_REFERENCE, ESCAPE_REPLACEMENTS_REFERENCE);
}

代码示例来源:origin: org.xwiki.rendering/xwiki-rendering-syntax-xwiki20

/**
   * @param text the query string and anchor parts to which to add escapes to
   * @return the modified text
   */
  protected String addEscapesToExtraParts(String text)
  {
    return StringUtils.replaceEach(text, ESCAPES_EXTRA, ESCAPE_REPLACEMENTS_EXTRA);
  }
}

代码示例来源:origin: de.vandermeer/ascii-utf-themes

@Override
public String getNumber(int number) {
  Validate.validState(0<number && number<4001, "numbering supported 0<number<4001 - number was: " + number);
  String literal = Integer_To_RomanLiteral.convert(number);
  literal = literal.replace("IV", "Ⅳ");
  literal = literal.replace("IX", "Ⅸ");
  return StringUtils.replaceEach(literal, from, to);
}

代码示例来源:origin: com.linkedin.gobblin/gobblin-data-management

/**
 * Resolve {@value #DATABASE_TOKEN} and {@value #TABLE_TOKEN} in <code>rawString</code> to {@link Table#getDbName()}
 * and {@link Table#getTableName()}
 */
public static String resolveTemplate(String rawString, Table table) {
 if (StringUtils.isBlank(rawString)) {
  return rawString;
 }
 return StringUtils.replaceEach(rawString, new String[] { DATABASE_TOKEN, TABLE_TOKEN }, new String[] { table.getDbName(), table.getTableName() });
}

代码示例来源:origin: de.vandermeer/ascii-utf-themes

@Override
public String getNumber(int number) {
  Validate.validState(0<number && number<4001, "numbering supported 0<number<4001 - number was: " + number);
  return StringUtils.replaceEach(Integer_To_RomanLiteral.convert(number), from, to);
}

代码示例来源:origin: org.xworker/xworker_core

public static String replaceEach(ActionContext actionContext){
  Thing self = actionContext.getObject("self");
  String text  = (String) self.doAction("getText", actionContext);
  String[] searchList  = (String[]) self.doAction("getSearchList", actionContext);
  String[] replacementList  = (String[]) self.doAction("getReplacementList", actionContext);
  return StringUtils.replaceEach(text, searchList, replacementList);
}

相关文章

微信公众号

最新文章

更多

StringUtils类方法