簡體   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