gpt4 book ai didi

java - 如何从字符串的开头修剪字符。安卓

转载 作者:行者123 更新时间:2023-11-29 22:10:49 25 4
gpt4 key购买 nike

我正在编写 Android 应用,需要一些帮助。

我有一个包含 URL 的字符串。有时我在 url 之前收到额外的文本,需要将其删除。

我得到这个“一些很酷的网站http://somecoolsite.com”

并想要这个“http://somecoolsite.com”

首先,我需要检测字符串是否以http://开头,如果不是,我需要修剪掉http://前面的所有内容

有没有简单的方法来做到这一点?

我可以做第一部分。

if (url.startsWith("http://") == false) {
url.replace("", replacement)
}

有什么帮助吗?

最佳答案

检查字符串是否以 http:// 开头

if (inputUrl.startsWith("http://")) {
...
}

要在 http:// 第一次出现之前删除前缀

int index = inputUrl.indexOf("http://");
if (index != -1)
inputUrl = inputUrl.substring(index);

API documentation for the String class应在此处为您提供所需的所有信息。

关于java - 如何从字符串的开头修剪字符。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9674769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com