繁体   English   中英

提取字符串直到空格前的点

[英]Extract string until dot before space

我有以下行:

Id.Action.AddToken Host Token
Id.Action.AddTokenFile path\to\file.ext 

我想匹配第一个空格之前的最后一个点之前的任何内容。 所以,在这种情况下,我想回来:

Id.Action.   (including the dot)

我已经尝试过([^\s]+)正则表达式来查找第一个空格,但是如何在它之前获取点?

如果您使用正则表达式,您将获得更好的性能(并且可以说代码更具可读性):

// First, search for the space, starting from the beginning of the string
int space = str.IndexOf(' ');
// Then, search backwards from the space until you find a dot
int dot = str.LastIndexOf('.', space);
// Get the substring (+ 1 to include the dot)
var result = str.Substring(0, dot + 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM