2

I have this string:

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

I want to go through it to find all // and / and their index.

I tried doing this with:

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

I was also thinking about using:

Foreach(char c in text)

but it will not work since // is not a single char.

How can I achieve what I want?

I tried this one also but did not display result

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));
 }

Thank you in advance.

3
  • 2
    What is the expected output for ///? {0}? {0, 1}? Something else? Commented Jan 19, 2011 at 3:43
  • 1
    I cleaned up the code formatting, grammar, and punctuation a bit in hopes of making it easier to understand. Commented Jan 19, 2011 at 3:49
  • I have tried this but does not work 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)); } Commented Jan 19, 2011 at 4:02

5 Answers 5

9

Simple:

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));

Output:

Found // at index 4.
Found // at index 11.
Found / at index 17.
Found / at index 25.
Sign up to request clarification or add additional context in comments.

Comments

4

Would it be possible using Split?

So:

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

And then go through the words? You would have blanks, due to the //, but that might be possible?

2 Comments

You can use StringSplitOptions.RemoveEmptyEntries so you don't get blanks, too.
In this case, you might want the blanks. Every blank (that's not the first or last in the array) will be between a pair of slashes.
2

If what you want is a list "book","title","page","section","para" you can use split.

    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"));

1 Comment

Actually what I need is not book,title,..so on .. I need / and // and its positions
1

Sometin like:

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

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

You can also do text.Split(@"//")

1 Comment

Timwi's regex is probably the better way to do this.
-2

You could replace // and / with your own words and then find the last index of

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);
        }

Remember to first replace double, otherwise you'll get SINGLESINGLE for // instead of DOUBLE. Hope this helps.

4 Comments

This would go pretty badly if the text were, say, "/SINGLE//SINGLE/DOUBLE/".
Agreed. Didn't think of that. I guess you could use regex
no it is good point since this will be bookdDOUBLEtitleDouble...etc but still the problem has not been solved which this will not loop to check all time of Double and Single appear in the text
Sorry didn't realise you wanted all indexes. I've update the code above to reflect your solution. It could be done in a cleaner way, but it gives you an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.