热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何用符号修剪/剪切java中的字符串?-Howtotrim/cutstringinjavabysymbol?

ImworkingonaprojectwheremyAPIreturnsurlwithidattheendofitandIwanttoextractit

I'm working on a project where my API returns url with id at the end of it and I want to extract it to be used in another function. Here's example url:

我正在开发一个项目,我的API在其结尾处返回带有id的url,我想将其解压缩以用于另一个函数。这是url的示例:

 String advertiserUrl = http://../../.../uuid/advertisers/4 <<

At the moment I'm using java's string function called substring() but this not the best approach as IDs could become 3 digit numbers and I would only get part of it. Heres my current approach:

目前我正在使用java的字符串函数substring(),但这不是最好的方法,因为ID可能会成为3位数字,我只会得到它的一部分。继承了我目前的做法:

String id = advertiserUrl.substring(advertiserUrl.length()-1,advertiserUrl.length());
System.out.println(id) //4

It works in this case but if id would be e.g "123" I would only get it as "3" after using substring, so my question is: is there a way to cut/trim string using dashes "/"? lets say theres 5 / in my current url so the string would get cut off after it detects fifth dash? Also any other sensible approach would be helpful too. Thanks.

它适用于这种情况,但如果id为例如“123”,我只会在使用子字符串后得到它为“3”,所以我的问题是:有没有办法用破折号“/”剪切/修剪字符串?让我说当前网址中的theres 5 /所以字符串在检测到第五个破折号后会被切断?任何其他合理的方法也会有所帮助。谢谢。

P.s uuid in url may vary in length too

url中的P.suuid也可能在长度上有所不同

3 个解决方案

#1


5  

You don't need to use regular expressions for this.

您不需要为此使用正则表达式。

Use String#lastIndexOf along with substring instead:

使用String#lastIndexOf和substring代替:

String advertiserUrl = "http://../../.../uuid/advertisers/4";// <<

Output

产量

4

Update

更新

To find the uuid value, you can use regular expressions:

要查找uuid值,可以使用正则表达式:

String advertiserUrl = "http://111.111.11.111:1111/api/ppppp/2f5d1a31-878a-438b-a03b-e9f51076074a/adver‌​tisers/9";
//                           | preceded by "/"
//                           |     | any non-"/" character, reluctantly quantified
//                           |     |     | followed by "/advertisers"
Pattern p = Pattern.compile("(?<=/)[^/]+?(?=/adver‌​tisers)");
Matcher m = p.matcher(advertiserUrl);
if (m.find()) {
    System.out.println(m.group());
}

Output

产量

2f5d1a31-878a-438b-a03b-e9f51076074a

2f5d1a31-878a-438B-A03B-e9f51076074a

#2


1  

You can either split the string on slashes and take the last position of the array returned, or use the lastIndexOf("/") to get the index of the last slash and then substring the rest of the string.

您可以在斜杠上拆分字符串并获取返回的数组的最后位置,或使用lastIndexOf(“/”)获取最后一个斜杠的索引,然后对字符串的其余部分进行子串。

#3


1  

Use the lastIndexOf() method, which returns the index of the last occurrence of the specified character.

使用lastIndexOf()方法,该方法返回指定字符最后一次出现的索引。

String id = advertiserUrl.substring(advertiserUrl.lastIndexOf('/') + 1, advertiserUrl.length());

推荐阅读
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 本文介绍了在Java中检查字符串是否仅包含数字的方法,包括使用正则表达式的示例代码,并提供了测试案例进行验证。同时还解释了Java中的字符转义序列的使用。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • splitjava的简单介绍
    本文目录一览:1、Javasplit方法2、 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • 本文介绍了一个Python函数same_set,用于判断两个相等长度的数组是否包含相同的元素。函数会忽略元素的顺序和重复次数,如果两个数组包含相同的元素,则返回1,否则返回0。文章还提供了函数的具体实现代码和样例输入输出。 ... [详细]
  • HashMap的扩容知识详解
    本文详细介绍了HashMap的扩容知识,包括扩容的概述、扩容条件以及1.7版本中的扩容方法。通过学习本文,读者可以全面了解HashMap的扩容机制,提升对HashMap的理解和应用能力。 ... [详细]
  • 上一章讲了如何制作数据集,接下来我们使用mmcls来实现多标签分类。 ... [详细]
  • 字符串匹配RabinKarp算法讲解
    问题描述:Rabin-Karp的预处理时间是O(m),匹配时间O((n-m1)m)既然与朴素算法的匹配时间一样,而且还多了一些预处理时间& ... [详细]
  • jdk 1.7 ConcurrentHashmap的底层原理
    ConcurrentMashmap与HashTable的区别ConcurrentMashmap和HashTable都是线程安全的。HashTable内部通过一个table[]来存 ... [详细]
author-avatar
热血洒在青春中
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有