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

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

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

StringUtils.isNumeric介绍

[英]Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.

null will return false. An empty CharSequence (length()=0) will return false.

Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.

StringUtils.isNumeric(null)   = false 
StringUtils.isNumeric("")     = false 
StringUtils.isNumeric("  ")   = false 
StringUtils.isNumeric("123")  = true 
StringUtils.isNumeric("\u0967\u0968\u0969")  = true 
StringUtils.isNumeric("12 3") = false 
StringUtils.isNumeric("ab2c") = false 
StringUtils.isNumeric("12-3") = false 
StringUtils.isNumeric("12.3") = false 
StringUtils.isNumeric("-123") = false 
StringUtils.isNumeric("+123") = false

[中]检查CharSequence是否仅包含Unicode数字。小数点不是Unicode数字,返回false。
null将返回false。空的CharSequence(length()=0)将返回false。
请注意,该方法不允许使用前导符号(正号或负号)。此外,如果字符串通过了数值测试,则在按整数解析时,它仍可能生成NumberFormatException。parseInt或Long。parseLong,例如,如果值分别超出int或long的范围。

StringUtils.isNumeric(null)   = false 
StringUtils.isNumeric("")     = false 
StringUtils.isNumeric("  ")   = false 
StringUtils.isNumeric("123")  = true 
StringUtils.isNumeric("\u0967\u0968\u0969")  = true 
StringUtils.isNumeric("12 3") = false 
StringUtils.isNumeric("ab2c") = false 
StringUtils.isNumeric("12-3") = false 
StringUtils.isNumeric("12.3") = false 
StringUtils.isNumeric("-123") = false 
StringUtils.isNumeric("+123") = false

代码示例

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

/**
 * <p>Checks whether the <code>String</code> contains only
 * digit characters.</p>
 *
 * <p><code>Null</code> and empty String will return
 * <code>false</code>.</p>
 *
 * @param str  the <code>String</code> to check
 * @return <code>true</code> if str contains only Unicode numeric
 */
public static boolean isDigits(final String str) {
  return StringUtils.isNumeric(str);
}

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

/**
 * Checks whether the given String is a UN M.49 numeric area code.
 *
 * @param str the String to check
 * @return true, is the given String is a UN M.49 numeric area code.
 */
private static boolean isNumericAreaCode(final String str) {
  return StringUtils.isNumeric(str) && str.length() == 3;
}

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

private boolean isWord(String token) {
 boolean isWord = true;
 if (token.isEmpty() || StringUtils.isNumeric(token)) {
  isWord = false;
 } else if (token.length() == 1) {
  char c = token.charAt(0);
  if (!Character.isLetter(c)) {
   isWord = false;
  }
 }
 return isWord;
}

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

private List<File> listFolders( File rootFolder )
{
  File[] files = fileSystem.listFiles( rootFolder );
  return files == null ? Collections.emptyList()
             : Stream.of( files )
              .filter( f -> fileSystem.isDirectory( f ) && StringUtils.isNumeric( f.getName() ) )
              .sorted( FILE_COMPARATOR )
              .collect( toList() );
}

代码示例来源:origin: Red5/red5-server

public void setMaxLifetime(String maxLifetime) {
  if (StringUtils.isNumeric(maxLifetime)) {
    SessionManager.maxLifetime = Long.valueOf(maxLifetime);
  } else {
    SessionManager.maxLifetime = PropertyConverter.convertStringToTimeMillis(maxLifetime);
  }
  log.debug("Max lifetime set to {} ms", SessionManager.maxLifetime);
}

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

@Override
 public boolean accept(final Path fileName) {
  // The path should start with dir/<prefix> and end with our suffix
  final String fileNameString = fileName.toString();
  if (!fileNameString.startsWith(prefixPathStr)) {
   return false;
  }
  if (walFileSuffix.isEmpty()) {
   // in the case of the null suffix, we need to ensure the filename ends with a timestamp.
   return org.apache.commons.lang3.StringUtils
     .isNumeric(fileNameString.substring(prefixPathStr.length()));
  } else if (!fileNameString.endsWith(walFileSuffix)) {
   return false;
  }
  return true;
 }
};

代码示例来源:origin: Netflix/zuul

public static Integer getContentLengthIfPresent(ZuulMessage msg)
{
  final String contentLengthValue = msg.getHeaders().getFirst(com.netflix.zuul.message.http.HttpHeaderNames.CONTENT_LENGTH);
  if (StringUtils.isNotEmpty(contentLengthValue) && StringUtils.isNumeric(contentLengthValue)) {
    try {
      return Integer.valueOf(contentLengthValue);
    }
    catch (NumberFormatException e) {
      LOG.info("Invalid Content-Length header value on request. " +
          "value = " + String.valueOf(contentLengthValue));
    }
  }
  return null;
}

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

static Path findOriginalBucket(FileSystem fs,
                Path directory,
                int bucket) throws IOException {
 for(FileStatus stat: fs.listStatus(directory)) {
  String name = stat.getPath().getName();
  String numberPart = name.substring(0, name.indexOf('_'));
  if (org.apache.commons.lang3.StringUtils.isNumeric(numberPart) &&
    Integer.parseInt(numberPart) == bucket) {
   return stat.getPath();
  }
 }
 throw new IllegalArgumentException("Can't find bucket " + bucket + " in " +
   directory);
}

代码示例来源:origin: galenframework/galen

public int getLogLevel() {
  String value = readProperty(GalenProperty.GALEN_LOG_LEVEL);
  if (StringUtils.isNumeric(value)) {
    return Integer.parseInt(value);
  }
  else return 10;
}

代码示例来源:origin: galenframework/galen

private Integer parseIntegerParameter(String name, String value) {
  if (StringUtils.isNumeric(value)) {
    return Integer.parseInt(value);
  }
  else throw new SyntaxException(name + " parameter should be integer: " + value);
}

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

private TypedEntity getTypedEntityFromServletPathId(String servletPath, String ceilingEntity) {
  int idBegIndex = servletPath.indexOf("/", 1);
  if (idBegIndex > 0) {
    String id = servletPath.substring(idBegIndex + 1, servletPath.length());
    if(StringUtils.isNumeric(id)) {
      Object objectEntity = genericEntityService.readGenericEntity(ceilingEntity, Long.valueOf(id));
      if (TypedEntity.class.isAssignableFrom(objectEntity.getClass())) {
        return (TypedEntity) objectEntity;
      }
    }
  }
  return null;
}

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

private int getMonthFromArguments(Map<String, String> args) {
 String monthStr = getRequired("month", args);
 int month;
 if (StringUtils.isNumeric(monthStr)) {
  month = Integer.parseInt(monthStr);
 } else {
  month = getMonth(monthStr);
 }
 return month - 1;
}

代码示例来源:origin: simpligility/android-maven-plugin

/**
 * This method extracts a port number from the serial number of a device.
 * It assumes that the device name is of format [xxxx-nnnn] where nnnn is the
 * port number.
 *
 * @param device The device to extract the port number from.
 * @return Returns the port number of the device
 */
private int extractPortFromDevice( IDevice device )
{
  String portStr = StringUtils.substringAfterLast( device.getSerialNumber(), "-" );
  if ( StringUtils.isNotBlank( portStr ) && StringUtils.isNumeric( portStr ) )
  {
    return Integer.parseInt( portStr );
  }
  //If the port is not available then return -1
  return -1;
}

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

private int getMonthFromArguments(Map<String, String> args) {
 String monthStr = getRequired("month", args);
 int month;
 if (StringUtils.isNumeric(monthStr)) {
  month = Integer.parseInt(monthStr);
 } else {
  month = getMonth(monthStr);
 }
 return month - 1;
}

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

private int getMonthFromArguments(Map<String, String> args) {
  String monthStr = getRequired("month", args);
  int month;
  if (StringUtils.isNumeric(monthStr)) {
   month = Integer.parseInt(monthStr);
  } else {
   month = getMonth(monthStr);
  }
  return month - 1;
 }
}

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

/**
 * Returns true if this qualified name identifies an
 * anonymous class.
 */
public boolean isAnonymousClass() {
  return !isLocalClass() && StringUtils.isNumeric(getClassSimpleName());
}

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

@Test
public void testIsNumeric() {
  assertFalse(StringUtils.isNumeric(null));
  assertFalse(StringUtils.isNumeric(""));
  assertFalse(StringUtils.isNumeric(" "));
  assertFalse(StringUtils.isNumeric("a"));
  assertFalse(StringUtils.isNumeric("A"));
  assertFalse(StringUtils.isNumeric("kgKgKgKgkgkGkjkjlJlOKLgHdGdHgl"));
  assertFalse(StringUtils.isNumeric("ham kso"));
  assertTrue(StringUtils.isNumeric("1"));
  assertTrue(StringUtils.isNumeric("1000"));
  assertTrue(StringUtils.isNumeric("\u0967\u0968\u0969"));
  assertFalse(StringUtils.isNumeric("\u0967\u0968 \u0969"));
  assertFalse(StringUtils.isNumeric("2.3"));
  assertFalse(StringUtils.isNumeric("10 00"));
  assertFalse(StringUtils.isNumeric("hkHKHik6iUGHKJgU7tUJgKJGI87GIkug"));
  assertFalse(StringUtils.isNumeric("_"));
  assertFalse(StringUtils.isNumeric("hkHKHik*khbkuh"));
  assertFalse(StringUtils.isNumeric("+123"));
  assertFalse(StringUtils.isNumeric("-123"));
}

代码示例来源:origin: Netflix/zuul

public static Integer getContentLengthIfPresent(ZuulMessage msg)
{
  final String contentLengthValue = msg.getHeaders().getFirst(com.netflix.zuul.message.http.HttpHeaderNames.CONTENT_LENGTH);
  if (StringUtils.isNotEmpty(contentLengthValue) && StringUtils.isNumeric(contentLengthValue)) {
    try {
      return Integer.valueOf(contentLengthValue);
    }
    catch (NumberFormatException e) {
      LOG.info("Invalid Content-Length header value on request. " +
          "value = " + String.valueOf(contentLengthValue));
    }
  }
  return null;
}

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

/**
 * If hostname is a.b.c and the port is 123, return a:123 instead of a.b.c:123.
 * @return if host looks like it is resolved -- not an IP -- then strip the domain portion
 * otherwise returns same as {@link #toString()}}
 */
public String toStringWithoutDomain() {
 String hostname = getHostname();
 String [] parts = hostname.split("\\.");
 if (parts.length > 1) {
  for (String part: parts) {
   if (!StringUtils.isNumeric(part)) {
    return Address.fromParts(parts[0], getPort()).toString();
   }
  }
 }
 return toString();
}

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

/**
 * buildId should only be given when caller is absolutely sure about the job instance
 * (makes sense in agent-uploading artifacts/properties scenario because agent won't run a job if its copied over(it only executes real jobs)) -JJ
 * <p>
 * This does not return pipelineLabel
 */
public JobIdentifier findJob(String pipelineName, String pipelineCounter, String stageName, String stageCounter, String buildName, Long buildId) {
  JobConfigIdentifier jobConfigIdentifier = goConfigService.translateToActualCase(new JobConfigIdentifier(pipelineName, stageName, buildName));
  PipelineIdentifier pipelineIdentifier;
  if (JobIdentifier.LATEST.equalsIgnoreCase(pipelineCounter)) {
    pipelineIdentifier = pipelineService.mostRecentPipelineIdentifier(jobConfigIdentifier.getPipelineName());
  } else if (StringUtils.isNumeric(pipelineCounter)) {
    pipelineIdentifier = pipelineService.findPipelineByNameAndCounter(pipelineName, Integer.parseInt(pipelineCounter)).getIdentifier();
  } else {
    throw new RuntimeException("Expected numeric pipeline counter but received '%s'" + pipelineCounter);
  }
  stageCounter = StringUtils.isEmpty(stageCounter) ? JobIdentifier.LATEST : stageCounter;
  StageIdentifier stageIdentifier = translateStageCounter(pipelineIdentifier, jobConfigIdentifier.getStageName(), stageCounter);
  JobIdentifier jobId;
  if (buildId == null) {
    jobId = jobResolverService.actualJobIdentifier(new JobIdentifier(stageIdentifier, jobConfigIdentifier.getJobName()));
  } else {
    jobId = new JobIdentifier(stageIdentifier, jobConfigIdentifier.getJobName(), buildId);
  }
  if (jobId == null) {
    //fix for #5739
    throw new JobNotFoundException(pipelineName, stageName, buildName);
  }
  return jobId;
}

相关文章

StringUtils类方法