取出字符串中数字的最大值

package 算法;

public class 取出字符串中数字的最大值 {
    public static void main(String[] args) {
        String a1="er23425tyeu5";
        String b1=a1.replace("\\D", "");//剔除非数字
        System.out.println(b1);
        int max=-1;
        if(b1!=null && b1!=""){
            for(int i=0;i<b1.length();i++){
                max=Integer.parseInt(b1.substring(i, i+1))>max?Integer.parseInt(b1.substring(i,i+1)):max;
                
            }
        }
        System.out.println("max="+max);
    }

}
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个实现该功能的 R 函数,使用正则表达式完成对字符串的提: ```r count_continuous_numbers <- function(str) { num_count <- 0 nums <- c() max_num <- -Inf min_num <- Inf # 使用正则表达式提连续数字 num_matches <- gregexpr("[0-9]+", str) for (i in seq_along(num_matches)) { match <- regmatches(str, num_matches[i]) if (length(match) > 0) { num_count <- num_count + 1 num <- as.numeric(match) nums <- c(nums, num) max_num <- max(max_num, num) min_num <- min(min_num, num) } } result <- list(num_count = num_count, nums = nums, max_num = max_num, min_num = min_num) return(result) } ``` 该函数首先初始化一些变量,包括数字计数器、数字数组、最大值和最小值(初始值分别为负无穷和正无穷)。然后,函数使用正则表达式 `"[0-9]+"` 提字符串的连续数字。对于每个匹配项,函数将数字计数器加 1,并将数字转换为数值型后加入数字数组,并更新最大值和最小值。最后,函数返回一个包含数字计数器、数字数组、最大值和最小值的列表。 以下是一个示例使用该函数的代码: ```r str <- "adec12548d,。、rwda4584114,。,dead5458842readesa568931dd,。!,。、、ed3258248gyhnj,。,。!!!,,u228585" result <- count_continuous_numbers(str) cat("连续数字的总数:", result$num_count, "\n") cat("数字:", result$nums, "\n") cat("最大值:", result$max_num, "\n") cat("最小值:", result$min_num, "\n") ``` 输出结果如下: ``` 连续数字的总数: 6 数字: 12548 4584114 5458842 568931 3258248 228585 最大值: 5458842 最小值: 12548 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老马识途2.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值