Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there.I have following method.this method search for getting count of '\' in instance of string. but it has error.
XML
/// <summary>
       /// This method returns count of specified character in a string.
       /// </summary>
       /// <param name="stringForProcessing">Determines instance of string class that contains specifiedCharacter</param>
       /// <param name="specifiedCharacter">Determines specified character for searching. </param>
       /// <returns></returns>
       private int GetCountOfOccurance(string stringForProcessing, char specifiedCharacter)
       {
           int result = 0;
           foreach (char letter in stringForProcessing)
           {
               if (letter.Equals(specifiedCharacter))
               {
                   result++;
               }
           }
           return result;
       }


and here i call it but it doesn't allow for compiling:
this.GetCountOfOccurance(path,'\')
Please help me. how can search for '\'
Posted

You should use the escape sequence for a backslash:
http://msdn.microsoft.com/en-us/library/h21280bw.aspx[^]
C#
this.GetCountOfOccurance(path, '\\');

Hope this helps.
 
Share this answer
 
Comments
aliwpf 30-Oct-13 6:08am    
It's wrong.
Thomas Daniels 30-Oct-13 6:16am    
Why is it wrong?
Two things:
First, you need to escape the backslash by using two of them:
C#
this.GetCountOfOccurance(path,'\\');
Since the backslash character is a "special" in C# which introduces other characters such as newline: '\n', quote: '\'' and doublequote: '\"'.

Secondly, there are a huge number of ways to do this: Counting Lines in a String[^] covers a good number of them!
 
Share this answer
 
Comments
aliwpf 30-Oct-13 6:08am    
it's wrong.
OriginalGriff 30-Oct-13 6:50am    
That is not a helpful error report: what is wrong? What's it doing that you don't expect, or not doing that you do? Is there an error message?
The backslash character smust be properly escaped, e.g.
the following line is valid
C#
char c = '\\'; // compiles fine


while the line below one is not
C#
char c = '\'; // COMPILATION ERROR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900