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

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

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

String.indexOf介绍

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

this.charAt(k) == ch

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

this.codePointAt(k) == ch

is true. In either case, if no such character occurs in this string, then -1 is returned.
[中]返回此字符串中指定字符第一次出现的索引。如果值为ch的字符出现在此String对象表示的字符序列中,则返回第一次出现的索引(以Unicode代码单位表示)。对于介于0到0xFFFF(含)之间的ch值,这是最小的值k,因此:

this.charAt(k) == ch

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

this.codePointAt(k) == ch

为真。在任何一种情况下,如果此字符串中没有出现此类字符,则返回-1

代码示例

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

@Override
 public boolean apply(ClassInfo info) {
  return info.className.indexOf('$') == -1;
 }
};

代码示例来源:origin: spring-projects/spring-framework

private int getEndPathIndex(String path) {
    int end = path.indexOf('?');
    int fragmentIndex = path.indexOf('#');
    if (fragmentIndex != -1 && (end == -1 || fragmentIndex < end)) {
      end = fragmentIndex;
    }
    if (end == -1) {
      end = path.length();
    }
    return end;
  }
}

代码示例来源:origin: square/okhttp

/**
 * Returns the index of the first character in {@code input} that contains a character in {@code
 * delimiters}. Returns limit if there is no such character.
 */
public static int delimiterOffset(String input, int pos, int limit, String delimiters) {
 for (int i = pos; i < limit; i++) {
  if (delimiters.indexOf(input.charAt(i)) != -1) return i;
 }
 return limit;
}

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

@Override
public int read() throws IOException {
 int readChar;
 do {
  readChar = delegate.read();
 } while (readChar != -1 && toIgnore.indexOf((char) readChar) >= 0);
 return readChar;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Try to consume the supplied token against the supplied content and update the
 * in comment parse state to the supplied value. Returns the index into the content
 * which is after the token or -1 if the token is not found.
 */
private int commentToken(String line, String token, boolean inCommentIfPresent) {
  int index = line.indexOf(token);
  if (index > - 1) {
    this.inComment = inCommentIfPresent;
  }
  return (index == -1 ? index : index + token.length());
}

代码示例来源:origin: spring-projects/spring-framework

private int getQueryIndex(String path) {
  int suffixIndex = path.length();
  int queryIndex = path.indexOf('?');
  if (queryIndex > 0) {
    suffixIndex = queryIndex;
  }
  int hashIndex = path.indexOf('#');
  if (hashIndex > 0) {
    suffixIndex = Math.min(suffixIndex, hashIndex);
  }
  return suffixIndex;
}

代码示例来源:origin: spring-projects/spring-framework

private int getEndPathIndex(String lookupPath) {
  int suffixIndex = lookupPath.length();
  int queryIndex = lookupPath.indexOf('?');
  if (queryIndex > 0) {
    suffixIndex = queryIndex;
  }
  int hashIndex = lookupPath.indexOf('#');
  if (hashIndex > 0) {
    suffixIndex = Math.min(suffixIndex, hashIndex);
  }
  return suffixIndex;
}

代码示例来源:origin: square/okhttp

/**
 * Returns the next index in {@code input} at or after {@code pos} that contains a character from
 * {@code characters}. Returns the input length if none of the requested characters can be found.
 */
public static int skipUntil(String input, int pos, String characters) {
 for (; pos < input.length(); pos++) {
  if (characters.indexOf(input.charAt(pos)) != -1) {
   break;
  }
 }
 return pos;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Extract the "raw" bean name from the given (potentially generated) bean name,
 * excluding any "#..." suffixes which might have been added for uniqueness.
 * @param name the potentially generated bean name
 * @return the raw bean name
 * @see #GENERATED_BEAN_NAME_SEPARATOR
 */
public static String originalBeanName(String name) {
  Assert.notNull(name, "'name' must not be null");
  int separatorIndex = name.indexOf(GENERATED_BEAN_NAME_SEPARATOR);
  return (separatorIndex != -1 ? name.substring(0, separatorIndex) : name);
}

代码示例来源:origin: square/okhttp

boolean matches(String hostname) {
 if (pattern.startsWith(WILDCARD)) {
  int firstDot = hostname.indexOf('.');
  return (hostname.length() - firstDot - 1) == canonicalHostname.length()
    && hostname.regionMatches(false, firstDot + 1, canonicalHostname, 0,
    canonicalHostname.length());
 }
 return hostname.equals(canonicalHostname);
}

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

private static String getSubmittedFileName(Part part) {
  for (String cd : part.getHeader("content-disposition").split(";")) {
    if (cd.trim().startsWith("filename")) {
      String fileName = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
      return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1); // MSIE fix.
    }
  }
  return null;
}

代码示例来源:origin: square/okhttp

/** Add an header line containing a field name, a literal colon, and a value. */
public Builder add(String line) {
 int index = line.indexOf(":");
 if (index == -1) {
  throw new IllegalArgumentException("Unexpected header: " + line);
 }
 return add(line.substring(0, index).trim(), line.substring(index + 1));
}

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

@Override
public boolean canDecode(CharSequence chars) {
 StringBuilder builder = new StringBuilder();
 for (int i = 0; i < chars.length(); i++) {
  char c = chars.charAt(i);
  if (separator.indexOf(c) < 0) {
   builder.append(c);
  }
 }
 return delegate.canDecode(builder);
}

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

@Override
int decodeTo(byte[] target, CharSequence chars) throws DecodingException {
 StringBuilder stripped = new StringBuilder(chars.length());
 for (int i = 0; i < chars.length(); i++) {
  char c = chars.charAt(i);
  if (separator.indexOf(c) < 0) {
   stripped.append(c);
  }
 }
 return delegate.decodeTo(target, stripped);
}

代码示例来源:origin: spring-projects/spring-framework

protected int extractLink(int index, char endChar, String content, Set<ContentChunkInfo> result) {
  int start = index + 1;
  int end = content.indexOf(endChar, start);
  result.add(new ContentChunkInfo(start, end, true));
  return end + 1;
}

代码示例来源:origin: spring-projects/spring-framework

public static Method findMethod(String desc, ClassLoader loader) {
  try {
    int lparen = desc.indexOf('(');
    int dot = desc.lastIndexOf('.', lparen);
    String className = desc.substring(0, dot).trim();
    String methodName = desc.substring(dot + 1, lparen).trim();
    return getClass(className, loader).getDeclaredMethod(methodName, parseTypes(desc, loader));
  }
  catch (ClassNotFoundException | NoSuchMethodException ex) {
    throw new CodeGenerationException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

public void setTouchy(String touchy) throws Exception {
  if (touchy.indexOf('.') != -1) {
    throw new Exception("Can't contain a .");
  }
  if (touchy.indexOf(',') != -1) {
    throw new NumberFormatException("Number format exception: contains a ,");
  }
  this.touchy = touchy;
}

代码示例来源:origin: spring-projects/spring-framework

public static Constructor findConstructor(String desc, ClassLoader loader) {
  try {
    int lparen = desc.indexOf('(');
    String className = desc.substring(0, lparen).trim();
    return getClass(className, loader).getConstructor(parseTypes(desc, loader));
  }
  catch (ClassNotFoundException | NoSuchMethodException ex) {
    throw new CodeGenerationException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testExceptionStackTrace() {
  JMSException jmsEx = new JMSException("could not connect");
  Exception innerEx = new Exception("host not found");
  jmsEx.setLinkedException(innerEx);
  JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
  StringWriter sw = new StringWriter();
  PrintWriter out = new PrintWriter(sw);
  springJmsEx.printStackTrace(out);
  String trace = sw.toString();
  assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testAdviceUsingJoinPoint() {
  ClassPathXmlApplicationContext bf = newContext("usesJoinPointAspect.xml");
  ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
  adrian1.getAge();
  AdviceUsingThisJoinPoint aspectInstance = (AdviceUsingThisJoinPoint) bf.getBean("aspect");
  //(AdviceUsingThisJoinPoint) Aspects.aspectOf(AdviceUsingThisJoinPoint.class);
  //assertEquals("method-execution(int TestBean.getAge())",aspectInstance.getLastMethodEntered());
  assertTrue(aspectInstance.getLastMethodEntered().indexOf("TestBean.getAge())") != 0);
}

相关文章

微信公众号

最新文章

更多