String 如何在特定字符之后和之前删除字符串
我有这个字符串:String 如何在特定字符之后和之前删除字符串,string,typescript,replace,char,substring,String,Typescript,Replace,Char,Substring,我有这个字符串:https://2352353252142dsbxcs35@github.com/happy.git 我想要得到结果:https://github.com/happy.git(第二个/之后和@之后没有随机字符串,但没有@) 现在我有这样的东西: var s = 'https://2352353252142dsbxcs35@github.com/happy.git'; var d = s.substring(s.indexOf('/')+2, s.indexOf('@')+1; s
https://2352353252142dsbxcs35@github.com/happy.git
我想要得到结果:https://github.com/happy.git
(第二个/
之后和@
之后没有随机字符串,但没有@
)
现在我有这样的东西:
var s = 'https://2352353252142dsbxcs35@github.com/happy.git';
var d = s.substring(s.indexOf('/')+2, s.indexOf('@')+1;
s = s.replace(d, "");
这是可行的,但我知道这是一个丑陋的解决方案。
最有效、更通用的解决方案是什么?试试以下方法:
const indexOfAtSign: number = receivedMessage.indexOf('@')+1
const httpsString: string = 'https://'
const trimmedString: string = s.slice(indexOfAtSign)
const requiredURL: string = httpsString.concat(trimmedString)
// Print this value of requiredURL wherever you want.
因此,我的代码所做的是,它获取@
的位置,并删除它前面的所有内容以及符号本身。然后使用slice()
函数,剩下我命名为trimmedString的部分。现在我已经预先定义了https字符串,如果我们现在只需要合并它们。完成:-)
我已经在我的电报机器人中尝试过了,下面是它的工作原理:
此链接中的字符数是否相同?@Dharmaraj-不,随机字符数。请稍候,我好像找到了什么。但一旦确认,您只需删除
https://
之后和@github.com
之前的随机字符即可。请检查我的答案,它对m非常有效。一定要让我知道它对你的作用。。