c# - 如何在字符串值之间获取字符串值
问题描述
“该示例适用于 C# - 值是 - 这里是值。” 我想从我尝试过的这个字符串值中获取值(这里是值),但是当字符串值中有两个(-)破折号并且无法获取值时,它会给我错误。我怎么能做到这一点,我已经尝试过。
String St = "The example is for C# - The value is -Default ID.";
int pFrom = St.IndexOf(" - ") + "- ".Length;
int pTo = St.LastIndexOf(" . ");
String result = St.Substring(pFrom, pTo - pFrom);
当句子中只有一个(-)时,它可以正常工作。如何从字符串中获取值(默认 ID)。
解决方案
您可以使用正则表达式模式[^-]*$
例子
var input = "The example is for C# - The value is -Default ID.";
var match = Regex.Match(input, @"[^-]*$");
if (match.Success)
Console.WriteLine(match.Value);
输出
Default ID.
解释
可视化
推荐阅读
- javascript - 无法读取未定义的属性“材料”。使用嵌套对象反应 useState
- php - 即使在同一页面上,PHP 会话变量也不会更新
- vsto - Powerpoint VSTO 加载项清单有哪些要求?
- javascript - Chrome 扩展中的 Ajax 发布请求
- python - 在 python3.7 中使用 group by 发送 mySql 查询时没有结果
- visual-studio-code - vscode如何禁用选项卡自动完成
- javascript - Java 有 .class 文件,它是可执行文件/二进制文件。同样,Python 或 Javascript 是什么?
- react-native - 无法解析模块“react-transform-hmr/lib/index.js”
- css - 从material-ui文档布局两个demo
- php - 如何在具有外键的模型中创建数据库条目 - 最佳实践?