[英]find the index of the first number that appears after a certain substring in c#
[英]Get index of first detected space after a certain index in a string
在要格式化的字符串中(主要是用不同的符號替換char以在UI上進行渲染測試),我必須檢測%,然后從此%char中跳過所有char util的第一個空格,並且必須對字符串中的所有實例重復該操作。
例如 abcd%1$s efgh %2$d ijkl
。在此字符串中,我必須獲取%的索引,然后從中找到第一個空格的索引。 基本上,我必須跳過%1$s
和%2$d
這是某種格式的占位符。 我希望,我不會在這里把它復雜化。
您可以很容易地獲得它,只需在第一個百分號前獲取索引,然后利用該索引從那里找到第一個空格:
var start = myString.IndexOf("%");
var spaceIndex = myString.IndexOf(" ", start)
當然,myString的值是您在問題中表示的字符串。
根據您的要求,以下是最簡單且可擴展的方法:“在此字符串中, abcd%1$s efgh %2$d ijkl
我必須跳過此%1$s
和%2$d
,它們是某種格式的占位符。”
string[] placeHolders = new string[] {"%1$s", "%2$d"};
string[] splits = "abcd%1$s efgh %2$d ijkl".Split(placeHolders, StringSplitOptions.None);
它將以["abcd", "efgh", "ijkl"]
提供splits
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.