2011-01-19 57 views
1

我有這個字符串:遍歷字符串,找到子

text = "book//title//page/section/para"; 

我想通過它來找到所有//和/和其索引。

我試圖這樣做:

if (text.Contains("//")) 
{ 
    Console.WriteLine(" // index: {0} ", text.IndexOf("//")); 
} 
if (text.Contains("/")) 
{ 
    Console.WriteLine("/ index: {0} :", text.IndexOf("/"));  
} 

我也在考慮使用:

Foreach(char c in text) 

,但它不會工作,因爲//不是一個單一的字符。

我怎樣才能達到我想要的?

我想這其中也但未顯示效果

string input = "book//title//page/section/para"; 
string pattern = @"\/\//"; 


Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); 
MatchCollection matches = rgx.Matches(input); 
if (matches.Count > 0) 
    { 
     Console.WriteLine("{0} ({1} matches):", input, matches.Count); 
     foreach (Match match in matches) 
     Console.WriteLine(" " + input.IndexOf(match.Value)); 
} 

預先感謝您。

+2

是什麼``///預期的輸出? `{0}`? `{0,1}`?還有別的嗎? – Ani 2011-01-19 03:43:14

+1

爲了更容易理解,我稍微清理了代碼格式,語法和標點符號。 – 2011-01-19 03:49:28

+0

我試過這個,但不起作用string input =「book // title // page/section/para」; string pattern = @「\/\ //」; Regex rgx = new Regex(pattern,RegexOptions.IgnoreCase); MatchCollection matches = rgx.Matches(input);如果(matches.Count> 0) Console.WriteLine(「{0}({1} matches):」,input,matches.Count); foreach(匹配匹配) Console.WriteLine(「」+ input。的IndexOf(match.Value)); } – Dena 2011-01-19 04:02:52

回答

3

是否有可能使用拆分?

所以:

string[] words = text.Split(@'/'); 

,然後再通過話?由於//,你會有空白,但這可能是可能的?

+2

可以使用StringSplitOptions.RemoveEmptyEntries所以你不要空白,太。 – 2011-01-19 03:45:14

+0

在這種情況下,您可能需要*空白。每一個空白(這不是數組中的第一個或最後一個)將位於一對斜線之間。 – cHao 2011-01-19 03:56:43

1

Sometin喜歡:

bool lastCharASlash = false; 
foreach(char c in text) 
{ 
    if(c == @'/') 
    { 
     if(lastCharASlash) 
     { 

      // my code... 
     } 
     lastCharASlash = true; 
    } 
    else lastCharASlash = false; 
} 

你也可以做text.Split(@"//")

2

如果你想要的是一個列表 「書」, 「標題」, 「網頁」, 「節」, 「對」 你可以使用拆分。

string text = "book//title//page/section/para"; 
    string[] delimiters = { "//", "/" }; 

    string[] result = text.Split(delimiters,StringSplitOptions.RemoveEmptyEntries); 
    System.Diagnostics.Debug.WriteLine(result); 
    Assert.IsTrue(result[0].isEqual("book")); 
    Assert.IsTrue(result[1].isEqual("title")); 
    Assert.IsTrue(result[2].isEqual("page")); 
    Assert.IsTrue(result[3].isEqual("section")); 
    Assert.IsTrue(result[4].isEqual("para")); 
-2

您可以取代//和/用你自己的話,然後找到

string s = "book//title//page/section/para"; 

     s = s.Replace("//", "DOUBLE"); 
     s = s.Replace("/", "SINGLE"); 

     IList<int> doubleIndex = new List<int>(); 

     while (s.Contains("DOUBLE")) 
     { 
      int index = s.IndexOf("DOUBLE"); 
      s = s.Remove(index, 6); 
      s = s.Insert(index, "//"); 
      doubleIndex.Add(index); 
     } 

     IList<int> singleIndex = new List<int>(); 

     while (s.Contains("SINGLE")) 
     { 
      int index = s.IndexOf("SINGLE"); 
      s = s.Remove(index, 6); 
      s = s.Insert(index, "/"); 
      singleIndex.Add(index); 
     } 

記得第一雙替換最後一個索引,否則你會得到SINGLESINGLE對於//,而不是DOUBLE 。希望這可以幫助。

6

簡單:

var text = "book//title//page/section/para"; 
foreach (Match m in Regex.Matches(text, "//?")) 
    Console.WriteLine(string.Format("Found {0} at index {1}.", m.Value, m.Index)); 

輸出:

Found // at index 4. 
Found // at index 11. 
Found/at index 17. 
Found/at index 25.