如何检查字符串是否以特定字符串开头和结尾? [英] How to check if a string starts and ends with specific strings?

查看:60
本文介绍了如何检查字符串是否以特定字符串开头和结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like:

string str = "https://abce/MyTest";

我想检查特定的字符串是否以 https://开头并以/MyTest 结尾.

I want to check if the particular string starts with https:// and ends with /MyTest.

我该如何做到?

推荐答案

此正则表达式:

^https://.*/MyTest$

将按照您的要求进行操作.

will do what you ask.

^ 匹配字符串的开头.

https://将会完全匹配.

.* 将匹配任何种类(.部分)的任意数量的字符( * 部分).如果要确保中间至少有一个字符,请改用.+ .

.* will match any number of characters (the * part) of any kind (the . part). If you want to make sure there is at least one character in the middle, use .+ instead.

/MyTest 完全匹配.

$ 匹配字符串的结尾.

要验证匹配,请使用:

Regex.IsMatch(str, @"^https://.*/MyTest$");

MSDN上的更多信息 Regex 页面.

More info at the MSDN Regex page.

这篇关于如何检查字符串是否以特定字符串开头和结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆