cn.hutool.core.util.StrUtil.subBefore()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(786)

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

StrUtil.subBefore介绍

[英]截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串
如果分隔字符串未找到,返回原字符串,举例如下:

StrUtil.subBefore(null, *)      = null 
StrUtil.subBefore("", *)        = "" 
StrUtil.subBefore("abc", 'a')   = "" 
StrUtil.subBefore("abcba", 'b') = "a" 
StrUtil.subBefore("abc", 'c')   = "ab" 
StrUtil.subBefore("abc", 'd')   = "abc"

[中]截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(空)或"")或者分隔字符串为无效的返回原字符串
如果分隔字符串未找到,返回原字符串,举例如下:

StrUtil.subBefore(null, *)      = null 
StrUtil.subBefore("", *)        = "" 
StrUtil.subBefore("abc", 'a')   = "" 
StrUtil.subBefore("abcba", 'b') = "a" 
StrUtil.subBefore("abc", 'c')   = "ab" 
StrUtil.subBefore("abc", 'd')   = "abc"

代码示例

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

/**
 * 截取文件绝对路径中包名之前的部分
 * 
 * @param file 文件
 * @return 包名之前的部分
 */
private String subPathBeforePackage(File file) {
  String filePath = file.getAbsolutePath();
  if (StrUtil.isNotEmpty(this.packageDirName)) {
    filePath = StrUtil.subBefore(filePath, this.packageDirName, true);
  }
  return StrUtil.addSuffixIfNot(filePath, File.separator);
}
// --------------------------------------------------------------------------------------------------- Private method end

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

/**
 * 截取文件绝对路径中包名之前的部分
 * 
 * @param file 文件
 * @return 包名之前的部分
 */
private String subPathBeforePackage(File file) {
  String filePath = file.getAbsolutePath();
  if (StrUtil.isNotEmpty(this.packageDirName)) {
    filePath = StrUtil.subBefore(filePath, this.packageDirName, true);
  }
  return StrUtil.addSuffixIfNot(filePath, File.separator);
}
// --------------------------------------------------------------------------------------------------- Private method end

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

/**
 * 解析转换数字字符串为long型数字,规则如下:
 * 
 * <pre>
 * 1、0x开头的视为16进制数字
 * 2、0开头的视为8进制数字
 * 3、空串返回0
 * 4、其它情况按照10进制转换
 * </pre>
 * 
 * @param number 数字,支持0x开头、0开头和普通十进制
 * @return long
 * @since 4.1.4
 */
public static long parseLong(String number) {
  if (StrUtil.isBlank(number)) {
    return 0;
  }
  // 对于带小数转换为整数采取去掉小数的策略
  number = StrUtil.subBefore(number, CharUtil.DOT, false);
  if (StrUtil.isEmpty(number)) {
    return 0;
  }
  if (number.startsWith("0x")) {
    // 0x04表示16进制数
    return Long.parseLong(number.substring(2), 16);
  }
  
  return Long.parseLong(removeNumberFlag(number));
}

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

/**
 * 解析转换数字字符串为long型数字,规则如下:
 * 
 * <pre>
 * 1、0x开头的视为16进制数字
 * 2、0开头的视为8进制数字
 * 3、空串返回0
 * 4、其它情况按照10进制转换
 * </pre>
 * 
 * @param number 数字,支持0x开头、0开头和普通十进制
 * @return long
 * @since 4.1.4
 */
public static long parseLong(String number) {
  if (StrUtil.isBlank(number)) {
    return 0;
  }
  // 对于带小数转换为整数采取去掉小数的策略
  number = StrUtil.subBefore(number, CharUtil.DOT, false);
  if (StrUtil.isEmpty(number)) {
    return 0;
  }
  if (number.startsWith("0x")) {
    // 0x04表示16进制数
    return Long.parseLong(number.substring(2), 16);
  }
  
  return Long.parseLong(removeNumberFlag(number));
}

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

number = StrUtil.subBefore(number, CharUtil.DOT, false);
if (StrUtil.isEmpty(number)) {
  return 0;

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

number = StrUtil.subBefore(number, CharUtil.DOT, false);
if (StrUtil.isEmpty(number)) {
  return 0;

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 截取文件绝对路径中包名之前的部分
 * 
 * @param file 文件
 * @return 包名之前的部分
 */
private String subPathBeforePackage(File file) {
  String filePath = file.getAbsolutePath();
  if (StrUtil.isNotEmpty(this.packageDirName)) {
    filePath = StrUtil.subBefore(filePath, this.packageDirName, true);
  }
  return StrUtil.addSuffixIfNot(filePath, File.separator);
}
// --------------------------------------------------------------------------------------------------- Private method end

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 解析转换数字字符串为long型数字,规则如下:
 * 
 * <pre>
 * 1、0x开头的视为16进制数字
 * 2、0开头的视为8进制数字
 * 3、空串返回0
 * 4、其它情况按照10进制转换
 * </pre>
 * 
 * @param number 数字,支持0x开头、0开头和普通十进制
 * @return long
 * @since 4.1.4
 */
public static long parseLong(String number) {
  if (StrUtil.isBlank(number)) {
    return 0;
  }
  // 对于带小数转换为整数采取去掉小数的策略
  number = StrUtil.subBefore(number, CharUtil.DOT, false);
  if (StrUtil.isEmpty(number)) {
    return 0;
  }
  if (number.startsWith("0x")) {
    // 0x04表示16进制数
    return Long.parseLong(number.substring(2), 16);
  }
  
  return Long.parseLong(removeNumberFlag(number));
}

代码示例来源:origin: cn.hutool/hutool-all

number = StrUtil.subBefore(number, CharUtil.DOT, false);
if (StrUtil.isEmpty(number)) {
  return 0;

代码示例来源:origin: xkcoding/spring-boot-demo

@PostMapping(value = "/local", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Dict local(@RequestParam("file") MultipartFile file) {
  if (file.isEmpty()) {
    return Dict.create().set("code", 400).set("message", "文件内容为空");
  }
  String fileName = file.getOriginalFilename();
  String rawFileName = StrUtil.subBefore(fileName, ".", true);
  String fileType = StrUtil.subAfter(fileName, ".", true);
  String localFilePath = StrUtil.appendIfMissing(fileTempPath, "/") + rawFileName + "-" + DateUtil.current(false) + "." + fileType;
  try {
    file.transferTo(new File(localFilePath));
  } catch (IOException e) {
    log.error("【文件上传至本地】失败,绝对路径:{}", localFilePath);
    return Dict.create().set("code", 500).set("message", "文件上传失败");
  }
  log.info("【文件上传至本地】绝对路径:{}", localFilePath);
  return Dict.create().set("code", 200).set("message", "上传成功").set("data", Dict.create().set("fileName", fileName).set("filePath", localFilePath));
}

代码示例来源:origin: xkcoding/spring-boot-demo

String rawFileName = StrUtil.subBefore(fileName, ".", true);
String fileType = StrUtil.subAfter(fileName, ".", true);
String localFilePath = StrUtil.appendIfMissing(fileTempPath, "/") + rawFileName + "-" + DateUtil.current(false) + "." + fileType;

相关文章

微信公众号

最新文章

更多

StrUtil类方法