我已經堅持了一段時間,我的輸出如下所示:前 2 個字母代表走廊編號,因此第一個字母為 17,接下來的數字代表貨架編號(在走廊中的位置)。正如您在走廊 17 的貨架 1 中看到的那樣,我們有 A1 或 A,但這並不重要。我希望 171A1 的輸出為 171,而 15211 的輸出為 1521,因此我想刪除末尾的字母以及後面可能跟隨的數字。
171A1
171A1
171A
171A0
15211
15211
15211
15210
15190
我嘗試使用 string.Remove(string.Length-2) 但這不起作用,例如我們有 171A,它應該變成 171。任何幫助將不勝感激。
如果您不需要它是 REGEX,這應該可以工作
var rawString = "15211";
var maxLength = 4;
var trimmedResult = new string(
rawString.TakeWhile(char.IsNumber) // loop through characters using LINQ. since your string starts with numbers, you can just keep taking until you bump into a character
.Take(maxLength) // limit the length for cases like "15211" where you don't need the extra numbers in the end
.ToArray() // so we can use the new string() constructor
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句