java.lang.String.replaceAll()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(174)

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

String.replaceAll介绍

[英]Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

java.util.regex.Pattern. java.util.regex.Pattern#compile(regex). java.util.regex.Pattern#matcher(java.lang.CharSequence)(str). java.util.regex.Matcher#replaceAll(repl)

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see java.util.regex.Matcher#replaceAll. Use java.util.regex.Matcher#quoteReplacement to suppress the special meaning of these characters, if desired.
[中]将此字符串中与给定regular expression匹配的每个子字符串替换为给定的替换项。
调用str.replaceAll(regex,repl)形式的此方法会产生与表达式完全相同的结果
JAVAutil。正则表达式。图案JAVAutil。正则表达式。模式#编译(regex)。JAVAutil。正则表达式。模式匹配器(java.lang.CharSequence)(str)。JAVAutil。正则表达式。Matcher#replaceAll(repl)
请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果不同于将其视为文字替换字符串时的结果;参见java。util。正则表达式。Matcher#replaceAll。使用java。util。正则表达式。Matcher“quoteReplacement,如果需要,用于抑制这些字符的特殊含义。

代码示例

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

public AssetDescriptor (String fileName, Class<T> assetType, AssetLoaderParameters<T> params) {
  this.fileName = fileName.replaceAll("\\\\", "/");
  this.type = assetType;
  this.params = params;
}

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

public AssetDescriptor (String fileName, Class<T> assetType, AssetLoaderParameters<T> params) {
  this.fileName = fileName.replaceAll("\\\\", "/");
  this.type = assetType;
  this.params = params;
}

代码示例来源:origin: stackoverflow.com

static String splitCamelCase(String s) {
  return s.replaceAll(
   String.format("%s|%s|%s",
     "(?<=[A-Z])(?=[A-Z][a-z])",
     "(?<=[^A-Z])(?=[A-Z])",
     "(?<=[A-Za-z])(?=[^A-Za-z])"
   ),
   " "
  );
}

代码示例来源:origin: shuzheng/zheng

/**
 * 驼峰转下划线(简单写法,效率低于{@link #humpToLine(String)})
 * @param str
 * @return
 */
public static String humpToLine2(String str) {
  return str.replaceAll("[A-Z]", "_$0").toLowerCase();
}

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

private static String normalize(String value) {
  return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
}

代码示例来源:origin: stackoverflow.com

String[] tests = {
   "  x  ",          // [x]
   "  1   2   3  ",  // [1 2 3]
   "",               // []
   "   ",            // []
 };
 for (String test : tests) {
   System.out.format("[%s]%n",
     test.replaceAll("^ +| +$|( )+", "$1")
   );
 }

代码示例来源:origin: alibaba/druid

public void output(StringBuffer buf) {
  if ((this.text == null) || (this.text.length() == 0)) {
    buf.append("NULL");
    return;
  }
  buf.append("N'");
  buf.append(this.text.replaceAll("'", "''"));
  buf.append("'");
}

代码示例来源:origin: google/guava

@Override
 public String apply(String value) {
  return value.replaceAll("[\\r\\n]", "");
 }
});

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

/**
   * This is used to convert a configuration nodePath into a key
   * TODO doc
   *
   * @param path
   * @return key (nodePath less the config root path)
   */
  private String pathToKey(String path) {
    if (StringUtils.isEmpty(path)) {
      return path;
    }
    return path.replace(rootPath + "/", "").replaceAll("/", ".");
  }
}

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

/**
   * This is used to convert a configuration nodePath into a key
   * TODO doc
   *
   * @param path
   * @return key (nodePath less the config root path)
   */
  private String pathToKey(String path) {
    if (StringUtils.isEmpty(path)) {
      return path;
    }
    return path.replace(rootPath + "/", "").replaceAll("/", ".");
  }
}

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

/** Creates an AssetDescriptor with an already resolved name. */
public AssetDescriptor (FileHandle file, Class<T> assetType, AssetLoaderParameters<T> params) {
  this.fileName = file.path().replaceAll("\\\\", "/");
  this.file = file;
  this.type = assetType;
  this.params = params;
}

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

/** Creates an AssetDescriptor with an already resolved name. */
public AssetDescriptor (FileHandle file, Class<T> assetType, AssetLoaderParameters<T> params) {
  this.fileName = file.path().replaceAll("\\\\", "/");
  this.file = file;
  this.type = assetType;
  this.params = params;
}

代码示例来源:origin: Tencent/tinker

private static String sanitizeName(RType rType, AaptResourceCollector resourceCollector, String rawName) {
  String sanitizeName = rawName.replaceAll("[.:]", "_");
  resourceCollector.putSanitizeName(rType, sanitizeName, rawName);
  return sanitizeName;
}

代码示例来源:origin: jenkinsci/jenkins

public String resolve(String name) {
    final String value = original.resolve(name);
    if (value == null) return null;
    // Substitute one backslash with two
    return value.replaceAll("\\\\", "\\\\\\\\");
  }
};

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

private static boolean isOsx0() {
  String osname = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US)
      .replaceAll("[^a-z0-9]+", "");
  boolean osx = osname.startsWith("macosx") || osname.startsWith("osx");
  if (osx) {
    logger.debug("Platform: MacOS");
  }
  return osx;
}

代码示例来源:origin: alibaba/druid

protected void printChars(String text) {
  if (text == null) {
    print0(ucase ? "NULL" : "null");
  } else {
    print('\'');
    int index = text.indexOf('\'');
    if (index >= 0) {
      text = text.replaceAll("'", "''");
    }
    print0(text);
    print('\'');
  }
}

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

@Override
public TextureAtlas load (AssetManager assetManager, String fileName, FileHandle file, TextureAtlasParameter parameter) {
  for (Page page : data.getPages()) {
    Texture texture = assetManager.get(page.textureFile.path().replaceAll("\\\\", "/"), Texture.class);
    page.texture = texture;
  }
   TextureAtlas atlas = new TextureAtlas(data);
   data = null;
   return atlas;
}

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

@Override
public TextureAtlas load (AssetManager assetManager, String fileName, FileHandle file, TextureAtlasParameter parameter) {
  for (Page page : data.getPages()) {
    Texture texture = assetManager.get(page.textureFile.path().replaceAll("\\\\", "/"), Texture.class);
    page.texture = texture;
  }
   TextureAtlas atlas = new TextureAtlas(data);
   data = null;
   return atlas;
}

代码示例来源:origin: google/guava

public void testToString() {
 for (String inputName : SOMEWHERE_UNDER_PS) {
  InternetDomainName domain = InternetDomainName.from(inputName);
  /*
   * We would ordinarily use constants for the expected results, but
   * doing it by derivation allows us to reuse the test case definitions
   * used in other tests.
   */
  String expectedName = Ascii.toLowerCase(inputName);
  expectedName = expectedName.replaceAll("[\u3002\uFF0E\uFF61]", ".");
  if (expectedName.endsWith(".")) {
   expectedName = expectedName.substring(0, expectedName.length() - 1);
  }
  assertEquals(expectedName, domain.toString());
 }
}

代码示例来源:origin: google/guava

public void testGetKeyMatchesString() {
 for (StandardSystemProperty property : StandardSystemProperty.values()) {
  String fieldName = property.name();
  String expected = Ascii.toLowerCase(fieldName).replaceAll("_", ".");
  assertEquals(expected, property.key());
 }
}

相关文章