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

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

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

String.codePointCount介绍

[英]Returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.
[中]返回此String的指定文本范围内的Unicode码点数。文本范围从指定的beginIndex开始,扩展到索引endIndex - 1处的char。因此,文本范围的长度(以chars为单位)为endIndex-beginIndex。文本范围内的未配对代理项每个都计为一个代码点。

代码示例

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

@Override
public int length()
{
  return value.codePointCount( 0, value.length() );
}

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

/**
 * Returns the number of Unicode code points in the specified text
 * range of this {@code CharSeq}. The text range begins at the
 * specified {@code beginIndex} and extends to the
 * {@code char} at index {@code endIndex - 1}. Thus the
 * length (in {@code char}s) of the text range is
 * {@code endIndex-beginIndex}. Unpaired surrogates within
 * the text range count as one code point each.
 *
 * @param beginIndex the index to the first {@code char} of
 *                   the text range.
 * @param endIndex   the index after the last {@code char} of
 *                   the text range.
 * @return the number of Unicode code points in the specified text
 * range
 * @throws IndexOutOfBoundsException if the
 *                                   {@code beginIndex} is negative, or {@code endIndex}
 *                                   is larger than the length of this {@code CharSeq}, or
 *                                   {@code beginIndex} is larger than {@code endIndex}.
 */
public int codePointCount(int beginIndex, int endIndex) {
  return back.codePointCount(beginIndex, endIndex);
}

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

public StringBuilder drainTo(final StringBuilder b) {
  try {
    return b.append(string, idx + offs, offs + len);
  } finally {
    offset += string.codePointCount(idx + offs, offs + len);
    idx = len;
  }
}

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

public StringBuilder drainTo(final StringBuilder b) {
  try {
    return b.append(string, idx + offs, offs + len);
  } finally {
    offset += string.codePointCount(idx + offs, offs + len);
    idx = len;
  }
}

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

public String drainToString() {
    try {
      return string.substring(idx + offs, offs + len);
    } finally {
      offset += string.codePointCount(idx + offs, offs + len);
      idx = len;
    }
  }
};

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

public String drainToString() {
    try {
      return string.substring(idx + offs, offs + len);
    } finally {
      offset += string.codePointCount(idx + offs, offs + len);
      idx = len;
    }
  }
}

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

public int getCharacterLength() {
 return value.codePointCount(0, value.length());
}

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

final int[] result = new int[s.codePointCount(0, s.length())];
int index = 0;
for (int i = 0; i < result.length; i++) {

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

@Override
public int getCharacterLength() {
 String strippedValue = getStrippedValue();
 return strippedValue.codePointCount(0, strippedValue.length());
}

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

public void dumpStringData(String s) throws IOException {
 System.out.println("                       " + s);
 System.out.println("        String.length: " + s.length());
 System.out.println("String.codePointCount: " + s.codePointCount(0, s.length()));
 System.out.println("            Utf8.size: " + Utf8.size(s));
 System.out.println("          UTF-8 bytes: " + ByteString.encodeUtf8(s).hex());
 System.out.println();
}

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

public static String enforceMaxLength(String val, int maxLength) {
 if (val == null) {
  return null;
 }
 String value = val;
 if (maxLength > 0) {
  int valLength = val.codePointCount(0, val.length());
  if (valLength > maxLength) {
   // Truncate the excess chars to fit the character length.
   // Also make sure we take supplementary chars into account.
   value = val.substring(0, val.offsetByCodePoints(0, maxLength));
  }
 }
 return value;
}

代码示例来源:origin: languagetool-org/languagetool

/**
 * Checks whether a given String consists only of surrogate pairs.
 * @param word to be checked
 * @since 4.2
 */
protected boolean isSurrogatePairCombination (String word) {
 if (word.length() > 1 && word.length() % 2 == 0 && word.codePointCount(0, word.length()) != word.length()) {
  // some symbols such as emojis (😂) have a string length that equals 2
  boolean isSurrogatePairCombination = true;
  for (int i = 0; i < word.length() && isSurrogatePairCombination; i += 2) {
   isSurrogatePairCombination &= Character.isSurrogatePair(word.charAt(i), word.charAt(i + 1));
  }
  return isSurrogatePairCombination;
 }
 return false;
}

代码示例来源:origin: looly/hutool

/**
 * 编码
 * 
 * @param text 文本
 * @return 密文
 */
public String encode(String text) {
  Assert.notNull(text, "Text should not be null.");
  
  text = text.toUpperCase();
  final StringBuilder morseBuilder = new StringBuilder();
  final int len = text.codePointCount(0, text.length());
  for (int i = 0; i < len; i++) {
    int codePoint = text.codePointAt(i);
    String word = alphabets.get(codePoint);
    if (word == null) {
      word = Integer.toBinaryString(codePoint);
    }
    morseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split);
  }
  return morseBuilder.toString();
}

代码示例来源:origin: looly/hutool

/**
 * 编码
 * 
 * @param text 文本
 * @return 密文
 */
public String encode(String text) {
  Assert.notNull(text, "Text should not be null.");
  
  text = text.toUpperCase();
  final StringBuilder morseBuilder = new StringBuilder();
  final int len = text.codePointCount(0, text.length());
  for (int i = 0; i < len; i++) {
    int codePoint = text.codePointAt(i);
    String word = alphabets.get(codePoint);
    if (word == null) {
      word = Integer.toBinaryString(codePoint);
    }
    morseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split);
  }
  return morseBuilder.toString();
}

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

public static String getPaddedValue(String val, int maxLength) {
 if (val == null) {
  return null;
 }
 if (maxLength < 0) {
  return val;
 }
 int valLength = val.codePointCount(0, val.length());
 if (valLength > maxLength) {
  return enforceMaxLength(val, maxLength);
 }
 if (maxLength > valLength) {
  // Make sure we pad the right amount of spaces; valLength is in terms of code points,
  // while StringUtils.rpad() is based on the number of java chars.
  int padLength = val.length() + (maxLength - valLength);
  val = StringUtils.rightPad(val, padLength);
 }
 return val;
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public void validate(final Processor<FullData, FullData> processor,
    final ProcessingReport report, final MessageBundle bundle,
    final FullData data)
    throws ProcessingException
  {
    final String value = data.getInstance().getNode().textValue();
    final int size = value.codePointCount(0, value.length());

    if (size < intValue)
      report.error(newMsg(data, bundle, "err.common.minLength.tooShort")
        .putArgument("value", value).putArgument("found", size)
        .putArgument(keyword, intValue));
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public void validate(final Processor<FullData, FullData> processor,
    final ProcessingReport report, final MessageBundle bundle,
    final FullData data)
    throws ProcessingException
  {
    final String value = data.getInstance().getNode().textValue();
    final int size = value.codePointCount(0, value.length());

    if (size > intValue)
      report.error(newMsg(data, bundle, "err.common.maxLength.tooLong")
        .putArgument("value", value).putArgument("found", size)
        .putArgument(keyword, intValue));
  }
}

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

public void testStringLength() throws Exception {
 int strLen = 20;
 int[] lengths = { 15, 20, 25 };
 // Try with supplementary characters
 for (int idx1 = 0; idx1 < lengths.length; ++idx1) {
  // Create random test string
  int curLen = lengths[idx1];
  String testString = createRandomSupplementaryCharString(curLen);
  assertEquals(curLen, testString.codePointCount(0, testString.length()));
  String enforcedString = HiveBaseChar.enforceMaxLength(testString, strLen);
  if (curLen <= strLen) {
   // No truncation needed
   assertEquals(testString, enforcedString);
  } else {
   // String should have been truncated.
   assertEquals(strLen, enforcedString.codePointCount(0, enforcedString.length()));
  }
 }
 assertNull(HiveBaseChar.enforceMaxLength(null, 0));
}

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

@Test
public void nextString()
{
  for ( int i = 0; i < ITERATIONS; i++ )
  {
    TextValue textValue = randomValues.nextTextValue( 10, 20 );
    String asString = textValue.stringValue();
    int length = asString.codePointCount( 0, asString.length() );
    assertThat( length, greaterThanOrEqualTo( 10 ) );
    assertThat( length, lessThanOrEqualTo( 20 ) );
  }
}

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

public void testGetPaddedValue() {
  int strLen = 20;
  int[] lengths = { 15, 20, 25 };
  for (int idx1 = 0; idx1 < lengths.length; ++idx1) {
   int curLen = lengths[idx1];
   // Random test string
   String testString = createRandomSupplementaryCharString(curLen);
   assertEquals(curLen, testString.codePointCount(0, testString.length()));
   String paddedString = HiveBaseChar.getPaddedValue(testString, strLen);
   assertEquals(strLen, paddedString.codePointCount(0, paddedString.length()));
  }

  assertEquals("abc       ", HiveBaseChar.getPaddedValue("abc", 10));
  assertEquals("abc       ", HiveBaseChar.getPaddedValue("abc ", 10));
  assertNull(HiveBaseChar.getPaddedValue(null, 0));
 }
}

相关文章

微信公众号

最新文章

更多