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

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

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

String.regionMatches介绍

[英]Tests if two string regions are equal.

A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent identical character sequences. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len. The result is false if and only if at least one of the following is true:

  • toffset is negative.
  • ooffset is negative.
  • toffset+len is greater than the length of this String object.
  • ooffset+len is greater than the length of the other argument.
  • There is some nonnegative integer k less than len such that: this.charAt(toffset+k) != other.charAt(ooffset+k)
    [中]测试两个字符串区域是否相等。
    将此字符串对象的子字符串与另一个参数的子字符串进行比较。如果这些子字符串表示相同的字符序列,则结果为真。要比较的字符串对象的子字符串从索引toffset开始,长度为len。要比较的其他字符串的子字符串从索引ooffset开始,长度为len。当且仅当以下至少一项为真时,结果为假:
    *toffset为阴性。
    *ooffset是负数。
    *toffset+len大于此字符串对象的长度。
    *ooffset+len大于另一个参数的长度。
    *有一些非负整数k小于len,这样:这个。字符(toffset+k)=其他的。字符(ooffset+k)

代码示例

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

/**
 * Checks if two strings have the same suffix of specified length
 *
 * @param s   string
 * @param p   string
 * @param len length of the common suffix
 * @return true if both s and p are not null and both have the same suffix. Otherwise - false
 */
public static boolean commonSuffixOfLength(String s, String p, int len) {
  return s != null && p != null && len >= 0 && s.regionMatches(s.length() - len, p, p.length() - len, len);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Checks if one string starts with another using the case-sensitivity rule.
 * <p>
 * This method mimics {@link String#startsWith(String)} but takes case-sensitivity
 * into account.
 *
 * @param str  the string to check, not null
 * @param start  the start to compare against, not null
 * @return true if equal using the case rules
 * @throws NullPointerException if either string is null
 */
public boolean checkStartsWith(final String str, final String start) {
  return str.regionMatches(!sensitive, 0, start, 0, start.length());
}

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

/**
 * Test if the given {@code String} starts with the specified prefix,
 * ignoring upper/lower case.
 * @param str the {@code String} to check
 * @param prefix the prefix to look for
 * @see java.lang.String#startsWith
 */
public static boolean startsWithIgnoreCase(@Nullable String str, @Nullable String prefix) {
  return (str != null && prefix != null && str.length() >= prefix.length() &&
      str.regionMatches(true, 0, prefix, 0, prefix.length()));
}

代码示例来源: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: commons-io/commons-io

/**
 * Checks if one string contains another at a specific index using the case-sensitivity rule.
 * <p>
 * This method mimics parts of {@link String#regionMatches(boolean, int, String, int, int)}
 * but takes case-sensitivity into account.
 *
 * @param str  the string to check, not null
 * @param strStartIndex  the index to start at in str
 * @param search  the start to search for, not null
 * @return true if equal using the case rules
 * @throws NullPointerException if either string is null
 */
public boolean checkRegionMatches(final String str, final int strStartIndex, final String search) {
  return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
}

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

/**
 * Test if the given {@code String} ends with the specified suffix,
 * ignoring upper/lower case.
 * @param str the {@code String} to check
 * @param suffix the suffix to look for
 * @see java.lang.String#endsWith
 */
public static boolean endsWithIgnoreCase(@Nullable String str, @Nullable String suffix) {
  return (str != null && suffix != null && str.length() >= suffix.length() &&
      str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length()));
}

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

/** Returns true if {@code string.substring(offset).startsWith(substring)}. */
private static boolean substringStartsWith(String string, int offset, String substring) {
 return string.regionMatches(offset, substring, 0, substring.length());
}

代码示例来源:origin: prestodb/presto

public int parseInto(
      ReadWritablePeriod period, String periodStr,
      int position, Locale locale) {
    if (periodStr.regionMatches(true, position, iText, 0, iText.length())) {
      return position + iText.length();
    }
    return ~position;
  }
}

代码示例来源:origin: commons-io/commons-io

/**
 * Checks if one string ends with another using the case-sensitivity rule.
 * <p>
 * This method mimics {@link String#endsWith} but takes case-sensitivity
 * into account.
 *
 * @param str  the string to check, not null
 * @param end  the end to compare against, not null
 * @return true if equal using the case rules
 * @throws NullPointerException if either string is null
 */
public boolean checkEndsWith(final String str, final String end) {
  final int endLen = end.length();
  return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen);
}

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

/**
 * Sets the URL target of this request.
 *
 * @throws IllegalArgumentException if {@code url} is not a valid HTTP or HTTPS URL. Avoid this
 * exception by calling {@link HttpUrl#parse}; it returns null for invalid URLs.
 */
public Builder url(String url) {
 if (url == null) throw new NullPointerException("url == null");
 // Silently replace web socket URLs with HTTP URLs.
 if (url.regionMatches(true, 0, "ws:", 0, 3)) {
  url = "http:" + url.substring(3);
 } else if (url.regionMatches(true, 0, "wss:", 0, 4)) {
  url = "https:" + url.substring(4);
 }
 return url(HttpUrl.get(url));
}

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

/**
 * Checks if two strings have the same suffix of specified length
 *
 * @param s   string
 * @param p   string
 * @param len length of the common suffix
 * @return true if both s and p are not null and both have the same suffix. Otherwise - false
 */
public static boolean commonSuffixOfLength(String s, String p, int len) {
  return s != null && p != null && len >= 0 && s.regionMatches(s.length() - len, p, p.length() - len, len);
}

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

/**
 * Simple function to match <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">DNS wildcard</a>.
 */
static boolean matches(String template, String hostName) {
  if (template.startsWith("*.")) {
    return template.regionMatches(2, hostName, 0, hostName.length())
      || commonSuffixOfLength(hostName, template, template.length() - 1);
  }
  return template.equals(hostName);
}

代码示例来源:origin: prestodb/presto

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: org.apache.commons/commons-lang3

@Override
  boolean invoke() {
    return data.source.regionMatches(data.ignoreCase, data.toffset, data.other, data.ooffset, data.len);
  }
}.run(data, "String");

代码示例来源:origin: prestodb/presto

public int parse(String periodStr, int position) {
  for (String text : iSuffixesSortedDescByLength) {
    if (periodStr.regionMatches(true, position, text, 0, text.length())) {
      if (!matchesOtherAffix(text.length(), periodStr, position)) {
        return position + text.length();
      }
    }
  }
  return ~position;
}

代码示例来源:origin: prestodb/presto

public int parse(String periodStr, int position) {
  String text = iText;
  int textLength = text.length();
  if (periodStr.regionMatches(true, position, text, 0, textLength)) {
    if (!matchesOtherAffix(textLength, periodStr, position)) {
      return position + textLength;
    }
  }
  return ~position;
}

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

/**
 * Simple function to match <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">DNS wildcard</a>.
 */
static boolean matches(String template, String hostName) {
  if (template.startsWith("*.")) {
    return template.regionMatches(2, hostName, 0, hostName.length())
      || commonSuffixOfLength(hostName, template, template.length() - 1);
  }
  return template.equals(hostName);
}

代码示例来源:origin: prestodb/presto

public int scan(String periodStr, final int position) {
  int sourceLength = periodStr.length();
  for (int pos = position; pos < sourceLength; pos++) {
    for (String text : iSuffixesSortedDescByLength) {
      if (periodStr.regionMatches(true, pos, text, 0, text.length())) {
        if (!matchesOtherAffix(text.length(), periodStr, pos)) {
          return pos;
        }
      }
    }
  }
  return ~position;
}

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

/**
 * Check whether the given FieldError matches the given field.
 * @param field the field that we are looking up FieldErrors for
 * @param fieldError the candidate FieldError
 * @return whether the FieldError matches the given field
 */
protected boolean isMatchingFieldError(String field, FieldError fieldError) {
  if (field.equals(fieldError.getField())) {
    return true;
  }
  // Optimization: use charAt and regionMatches instead of endsWith and startsWith (SPR-11304)
  int endIndex = field.length() - 1;
  return (endIndex >= 0 && field.charAt(endIndex) == '*' &&
      (endIndex == 0 || field.regionMatches(0, fieldError.getField(), 0, endIndex)));
}

代码示例来源:origin: joda-time/joda-time

public int parse(String periodStr, int position) {
  for (String text : iSuffixesSortedDescByLength) {
    if (periodStr.regionMatches(true, position, text, 0, text.length())) {
      if (!matchesOtherAffix(text.length(), periodStr, position)) {
        return position + text.length();
      }
    }
  }
  return ~position;
}

相关文章

微信公众号

最新文章

更多