How to count a string for \ character?
-
Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:
public void DoYourThingBaby()
{
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
StreamReader sr = new StreamReader(pot);
string tempF = sr.ReadToEnd();
sr.Close();char\[\] tempC = tempF.ToCharArray();
// this
int stevc=0;
foreach(char chr in tempC)
if(chr=='\')
stevc++;// or this
label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
}the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance
Regards, Matjaž
-
Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:
public void DoYourThingBaby()
{
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
StreamReader sr = new StreamReader(pot);
string tempF = sr.ReadToEnd();
sr.Close();char\[\] tempC = tempF.ToCharArray();
// this
int stevc=0;
foreach(char chr in tempC)
if(chr=='\')
stevc++;// or this
label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
}the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance
Regards, Matjaž
Wild guess - something to do with Unicode/CodePage? If you open the file up using the binary editor is every other character zero?
Regards, Rob Philpott.
-
Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:
public void DoYourThingBaby()
{
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
StreamReader sr = new StreamReader(pot);
string tempF = sr.ReadToEnd();
sr.Close();char\[\] tempC = tempF.ToCharArray();
// this
int stevc=0;
foreach(char chr in tempC)
if(chr=='\')
stevc++;// or this
label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
}the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance
Regards, Matjaž
Ok. Dont know why my solution didnt work... but i've modified it a bit - a bit of a hax :P If i putted '\\' char to search... it found something. What did it found - i dont know... but it wasn't right. So, i've tried this - created a new textbox and enter one character inside - the '\'. And with this IT WORKED. So, here is the new code, that works if anyone's intrested. PS: thanks for your reply Rob.
public void DoYourThingBaby()
{string pot = @Application.StartupPath + @"\\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close(); char\[\] tempC = tempF.ToCharArray(); //textBox1.Text = tempF; int stevc=0; foreach(char chr in tempC) if(chr==Convert.ToChar(textBox1.Text)) stevc++; }
Regards, Matjaž
-
Ok. Dont know why my solution didnt work... but i've modified it a bit - a bit of a hax :P If i putted '\\' char to search... it found something. What did it found - i dont know... but it wasn't right. So, i've tried this - created a new textbox and enter one character inside - the '\'. And with this IT WORKED. So, here is the new code, that works if anyone's intrested. PS: thanks for your reply Rob.
public void DoYourThingBaby()
{string pot = @Application.StartupPath + @"\\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close(); char\[\] tempC = tempF.ToCharArray(); //textBox1.Text = tempF; int stevc=0; foreach(char chr in tempC) if(chr==Convert.ToChar(textBox1.Text)) stevc++; }
Regards, Matjaž
Can I ask what the point in the final 3 lines are below?
Matjaž Grahek wrote:
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close();
Seems a bit of a WTF to me.
-
Can I ask what the point in the final 3 lines are below?
Matjaž Grahek wrote:
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close();
Seems a bit of a WTF to me.
Bah... it IS WTF :) but its easier for my understanding... dont even bother with this. Programming is cool because of this - you have soooooo many different ways to make a program work.
Regards, Matjaž
-
Bah... it IS WTF :) but its easier for my understanding... dont even bother with this. Programming is cool because of this - you have soooooo many different ways to make a program work.
Regards, Matjaž
Just
Split()
the string on the char you want to count, the count will be the number of resulting array elements minus 1 (have to escape the backslash as'\\'
). Most likely your results were thrown off by the escaping of special characters in the text (CRLF becomes\r\n
, TAB becomes\t
, etc.) and perhaps it brought them over into thechar[]
in that manner. Either way, this will do the trick:public void DoYourThingBaby()
{
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
if(!System.IO.File.Exists(pot))
return;
int count = System.IO.File.ReadAllText(pot).Split('\\').Length - 1
}Keep It Simple Stupid! (KISS)
-
Just
Split()
the string on the char you want to count, the count will be the number of resulting array elements minus 1 (have to escape the backslash as'\\'
). Most likely your results were thrown off by the escaping of special characters in the text (CRLF becomes\r\n
, TAB becomes\t
, etc.) and perhaps it brought them over into thechar[]
in that manner. Either way, this will do the trick:public void DoYourThingBaby()
{
string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
if(!System.IO.File.Exists(pot))
return;
int count = System.IO.File.ReadAllText(pot).Split('\\').Length - 1
}Keep It Simple Stupid! (KISS)
You can use LINQ to accomplish the same thing in a more expressive manner:
int bkslashCount = 0;
string pot = Path.Combine(Application.StartupPath, lstFILES.SelectedItem); // USE Path.Combine!!
if (File.Exists(pot))
{
string data = File.ReadAllText(pot);
bkslashCount = data.ToCharArray().Where(c => c == '/').Count();
} -
You can use LINQ to accomplish the same thing in a more expressive manner:
int bkslashCount = 0;
string pot = Path.Combine(Application.StartupPath, lstFILES.SelectedItem); // USE Path.Combine!!
if (File.Exists(pot))
{
string data = File.ReadAllText(pot);
bkslashCount = data.ToCharArray().Where(c => c == '/').Count();
}Hi, probably a lot cheaper is
count=str.Length-str.Replace("\\","").Length;
:)Luc Pattyn [Forum Guidelines] [My Articles]
Good riddance W
modified on Friday, June 10, 2011 12:07 PM