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

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

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

StringUtils.indexOf介绍

[英]Returns the index within seq of the first occurrence of the specified character. If a character with value searchChar occurs in the character sequence represented by seq``CharSequence object, then the index (in Unicode code units) of the first such occurrence is returned. For values of searchChar in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:

this.charAt(k) == searchChar

is true. For other values of searchChar, it is the smallest value k such that:

this.codePointAt(k) == searchChar

is true. In either case, if no such character occurs in seq, then INDEX_NOT_FOUND (-1) is returned.

Furthermore, a null or empty ("") CharSequence will return INDEX_NOT_FOUND (-1).

StringUtils.indexOf(null, *)         = -1 
StringUtils.indexOf("", *)           = -1 
StringUtils.indexOf("aabaabaa", 'a') = 0 
StringUtils.indexOf("aabaabaa", 'b') = 2

[中]返回指定字符第一次出现时seq内的索引。如果值为searchChar的字符出现在seq``CharSequence对象表示的字符序列中,则返回第一次出现的索引(以Unicode代码单位表示)。对于介于0到0xFFFF(含)之间的searchChar值,这是最小的值k,因此:

this.charAt(k) == searchChar

为真。对于searchChar的其他值,它是最小的值k,因此:

this.codePointAt(k) == searchChar

为真。在这两种情况下,如果seq中没有出现这样的字符,则返回INDEX_NOT_FOUND(-1)。
此外,null或空(“”)字符序列将返回未找到的索引(-1)。

StringUtils.indexOf(null, *)         = -1 
StringUtils.indexOf("", *)           = -1 
StringUtils.indexOf("aabaabaa", 'a') = 0 
StringUtils.indexOf("aabaabaa", 'b') = 2

代码示例

代码示例来源:origin: springside/springside4

/**
 * 在Windows环境里,兼容Windows上的路径分割符,将 '/' 转回 '\'
 */
public static String normalizePath(String path) {
  if (Platforms.FILE_PATH_SEPARATOR_CHAR == Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR
      && StringUtils.indexOf(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR) != -1) {
    return StringUtils.replaceChars(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR,
        Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR);
  }
  return path;
}

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

/**
 * Extracts the username from the specified DN. If the username cannot be extracted because the CN is in an unrecognized format, the entire CN is returned. If the CN cannot be extracted because
 * the DN is in an unrecognized format, the entire DN is returned.
 *
 * @param dn the dn to extract the username from
 * @return the exatracted username
 */
public static String extractUsername(String dn) {
  String username = dn;
  // ensure the dn is specified
  if (StringUtils.isNotBlank(dn)) {
    // determine the separate
    final String separator = StringUtils.indexOfIgnoreCase(dn, "/cn=") > 0 ? "/" : ",";
    // attempt to locate the cd
    final String cnPattern = "cn=";
    final int cnIndex = StringUtils.indexOfIgnoreCase(dn, cnPattern);
    if (cnIndex >= 0) {
      int separatorIndex = StringUtils.indexOf(dn, separator, cnIndex);
      if (separatorIndex > 0) {
        username = StringUtils.substring(dn, cnIndex + cnPattern.length(), separatorIndex);
      } else {
        username = StringUtils.substring(dn, cnIndex + cnPattern.length());
      }
    }
  }
  return username;
}

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

public static CurrentStatus getProjectBuildStatus(String statusStr) {
  for (CurrentStatus status : STATUSES) {
    if (StringUtils.indexOf(statusStr, status.getCruiseStatus()) == 0) {
      return status;
    }
  }
  return DISCONTINUED;
}

代码示例来源:origin: code4craft/webmagic

public static String getDomain(String url) {
  String domain = removeProtocol(url);
  int i = StringUtils.indexOf(domain, "/", 1);
  if (i > 0) {
    domain = StringUtils.substring(domain, 0, i);
  }
  return removePort(domain);
}

代码示例来源:origin: vipshop/vjtools

ndx = StringUtils.indexOf(field, SPECIAL_CHARS);

代码示例来源:origin: springside/springside4

ndx = StringUtils.indexOf(field, SPECIAL_CHARS);

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

@Test
public void testIndexOf_String() {
  assertEquals(-1, StringUtils.indexOf(null, null));
  assertEquals(-1, StringUtils.indexOf("", null));
  assertEquals(0, StringUtils.indexOf("", ""));
  assertEquals(0, StringUtils.indexOf("aabaabaa", "a"));
  assertEquals(2, StringUtils.indexOf("aabaabaa", "b"));
  assertEquals(1, StringUtils.indexOf("aabaabaa", "ab"));
  assertEquals(0, StringUtils.indexOf("aabaabaa", ""));
  assertEquals(2, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b"));
}

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

@Test
public void testIndexOf_char() {
  assertEquals(-1, StringUtils.indexOf(null, ' '));
  assertEquals(-1, StringUtils.indexOf("", ' '));
  assertEquals(0, StringUtils.indexOf("aabaabaa", 'a'));
  assertEquals(2, StringUtils.indexOf("aabaabaa", 'b'));
  assertEquals(2, StringUtils.indexOf(new StringBuilder("aabaabaa"), 'b'));
}

代码示例来源:origin: alipay/sofa-rpc

private ConsulURL buildURL(ConsulService service) {
  try {
    for (String tag : service.getTags()) {
      if (org.apache.commons.lang3.StringUtils.indexOf(tag, ConsulConstants.PROVIDERS_CATEGORY) != -1) {
        String toUrlPath = org.apache.commons.lang3.StringUtils.substringAfter(tag,
          ConsulConstants.PROVIDERS_CATEGORY);
        ConsulURL consulUrl = ConsulURL.valueOf(ConsulURL.decode(toUrlPath));
        return consulUrl;
      }
    }
  } catch (Exception e) {
    LOGGER.error("convert consul service to url fail! service:" + service, e);
  }
  return null;
}

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

@Test
public void testIndexOf_StringInt() {
  assertEquals(-1, StringUtils.indexOf(null, null, 0));
  assertEquals(-1, StringUtils.indexOf(null, null, -1));
  assertEquals(-1, StringUtils.indexOf(null, "", 0));
  assertEquals(-1, StringUtils.indexOf(null, "", -1));
  assertEquals(-1, StringUtils.indexOf("", null, 0));
  assertEquals(-1, StringUtils.indexOf("", null, -1));
  assertEquals(0, StringUtils.indexOf("", "", 0));
  assertEquals(0, StringUtils.indexOf("", "", -1));
  assertEquals(0, StringUtils.indexOf("", "", 9));
  assertEquals(0, StringUtils.indexOf("abc", "", 0));
  assertEquals(0, StringUtils.indexOf("abc", "", -1));
  assertEquals(3, StringUtils.indexOf("abc", "", 9));
  assertEquals(3, StringUtils.indexOf("abc", "", 3));
  assertEquals(0, StringUtils.indexOf("aabaabaa", "a", 0));
  assertEquals(2, StringUtils.indexOf("aabaabaa", "b", 0));
  assertEquals(1, StringUtils.indexOf("aabaabaa", "ab", 0));
  assertEquals(5, StringUtils.indexOf("aabaabaa", "b", 3));
  assertEquals(-1, StringUtils.indexOf("aabaabaa", "b", 9));
  assertEquals(2, StringUtils.indexOf("aabaabaa", "b", -1));
  assertEquals(2,StringUtils.indexOf("aabaabaa", "", 2));
  // Test that startIndex works correctly, i.e. cannot match before startIndex
  assertEquals(7, StringUtils.indexOf("12345678", "8", 5));
  assertEquals(7, StringUtils.indexOf("12345678", "8", 6));
  assertEquals(7, StringUtils.indexOf("12345678", "8", 7)); // 7 is last index
  assertEquals(-1, StringUtils.indexOf("12345678", "8", 8));
  assertEquals(5, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b", 3));
}

代码示例来源:origin: alipay/sofa-rpc

private ConsulURL buildURL(ConsulService service) {
  try {
    for (String tag : service.getTags()) {
      if (org.apache.commons.lang3.StringUtils.indexOf(tag, ConsulConstants.PROVIDERS_CATEGORY) != -1) {
        String toUrlPath = org.apache.commons.lang3.StringUtils.substringAfter(tag,
          ConsulConstants.PROVIDERS_CATEGORY);
        ConsulURL consulUrl = ConsulURL.valueOf(ConsulURL.decode(toUrlPath));
        return consulUrl;
      }
    }
  } catch (Exception e) {
    LOGGER.error("convert consul service to url fail! service:" + service, e);
  }
  return null;
}

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

@Test
public void testIndexOf_charInt() {
  assertEquals(-1, StringUtils.indexOf(null, ' ', 0));
  assertEquals(-1, StringUtils.indexOf(null, ' ', -1));
  assertEquals(-1, StringUtils.indexOf("", ' ', 0));
  assertEquals(-1, StringUtils.indexOf("", ' ', -1));
  assertEquals(0, StringUtils.indexOf("aabaabaa", 'a', 0));
  assertEquals(2, StringUtils.indexOf("aabaabaa", 'b', 0));
  assertEquals(5, StringUtils.indexOf("aabaabaa", 'b', 3));
  assertEquals(-1, StringUtils.indexOf("aabaabaa", 'b', 9));
  assertEquals(2, StringUtils.indexOf("aabaabaa", 'b', -1));
  assertEquals(5, StringUtils.indexOf(new StringBuilder("aabaabaa"), 'b', 3));
  StringBuilder builder = new StringBuilder();
  builder.appendCodePoint(CODE_POINT);
  assertEquals(0, StringUtils.indexOf(builder, CODE_POINT, 0));
  assertEquals(0, StringUtils.indexOf(builder.toString(), CODE_POINT, 0));
  builder.appendCodePoint(CODE_POINT);
  assertEquals(2, StringUtils.indexOf(builder, CODE_POINT, 1));
  assertEquals(2, StringUtils.indexOf(builder.toString(), CODE_POINT, 1));
  assertEquals(-1, StringUtils.indexOf(builder, CODE_POINT, 0));
  assertEquals(-1, StringUtils.indexOf(builder.toString(), CODE_POINT, 0));
  builder.appendCodePoint(CODE_POINT);
  assertEquals(1, StringUtils.indexOf(builder, CODE_POINT, 0));
  assertEquals(1, StringUtils.indexOf(builder.toString(), CODE_POINT, 0));
  assertEquals(-1, StringUtils.indexOf(builder, CODE_POINT, 2));
  assertEquals(-1, StringUtils.indexOf(builder.toString(), CODE_POINT, 2));

代码示例来源:origin: com.github.mg365/mg-groovy

/**  
 * 获取字符 quoteChar 在 line中出现的位置
 * @author liukefu
 * @param line
 * @param quoteChar
 * @param startFrom
 * @return
 */
public static int indexOf(String line,String quoteChar,int startFrom){
  
  return StringUtils.indexOf(line, quoteChar, startFrom);
}

代码示例来源:origin: org.kuali.common/kuali-util

public static final boolean containsUnresolvedPlaceholder(String string, String prefix, String suffix) {
  int beginIndex = StringUtils.indexOf(string, prefix);
  if (beginIndex == -1) {
    return false;
  }
  return StringUtils.indexOf(string, suffix) != -1;
}

代码示例来源:origin: metatron-app/metatron-discovery

private String parseProgressIndicatedLog(String log){
 int firstMapString = StringUtils.indexOf(log, "Map");
 int firstReducerString = StringUtils.indexOf(log, "Reducer");
 if(firstMapString > 0 && firstReducerString > 0){
  return StringUtils.substring(log, Math.min(firstMapString, firstReducerString), log.length());
 } else if(firstMapString > 0){
  return StringUtils.substring(log, firstMapString, log.length());
 } else if(firstReducerString > 0){
  return StringUtils.substring(log, firstReducerString, log.length());
 }
 return log;
}

代码示例来源:origin: xuminwlt/j360-dubbo-app-all

/**
 * 在Windows环境里,兼容Windows上的路径分割符,将 '/' 转回 '\'
 */
public static String normalizePath(String path) {
  if (Platforms.FILE_PATH_SEPARATOR_CHAR == Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR
      && StringUtils.indexOf(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR) != -1) {
    return StringUtils.replaceChars(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR,
        Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR);
  }
  return path;
}

代码示例来源:origin: io.springside/springside-utils

/**
 * 在Windows环境里,兼容Windows上的路径分割符,将 '/' 转回 '\'
 */
public static String normalizePath(String path) {
  if (Platforms.FILE_PATH_SEPARATOR_CHAR == Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR
      && StringUtils.indexOf(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR) != -1) {
    return StringUtils.replaceChars(path, Platforms.LINUX_FILE_PATH_SEPARATOR_CHAR,
        Platforms.WINDOWS_FILE_PATH_SEPARATOR_CHAR);
  }
  return path;
}

代码示例来源:origin: net.sf.sido/sido-schema

@Override
public SidoType getType(String qualifiedName, boolean required) {
  // Gets the schema name and the type name
  int pos = StringUtils.indexOf(qualifiedName, SCHEMA_SEPARATOR);
  if (pos < 0) {
    throw new SidoWrongQualifiedNameException(qualifiedName);
  }
  String schemaName = StringUtils.substringBefore(qualifiedName, SCHEMA_SEPARATOR);
  String typeName = StringUtils.substringAfter(qualifiedName, SCHEMA_SEPARATOR);
  return getType (schemaName, typeName, required);
}

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

public static int indexOf(ActionContext actionContext){
  Thing self = actionContext.getObject("self");
  CharSequence cs1  = (CharSequence) self.doAction("getCs1", actionContext);
  CharSequence cs2  = (CharSequence) self.doAction("getCs2", actionContext);
  Integer startPos = (Integer) self.doAction("getStartPos", actionContext);
  return StringUtils.indexOf(cs1, cs2, startPos);
}

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

private int getMinIndexOfJoinType() {
 int minJoinTypeIndex = Integer.MAX_VALUE;
 for (JoinType joinType : JoinType.values()) {
  int joinIndex = StringUtils.indexOf(trimmedQuery, joinType.name());
  if (joinIndex == -1) {
   continue;
  }
  minJoinTypeIndex = joinIndex < minJoinTypeIndex ? joinIndex : minJoinTypeIndex;
 }
 return minJoinTypeIndex == Integer.MAX_VALUE ? -1 : minJoinTypeIndex;
}

相关文章

微信公众号

最新文章

更多

StringUtils类方法