Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have string
str="123 456";

between 3 and 4 there are spaces and tabs ,how can i decern
the spaces from tabs

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ques
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pp();
        }

        private void pp()
        {
            int i,SumTab, SumSpace, len;
            string str;
            char[] characterArray = new char[250];

            str = "123       456";
            SumTab = 0;
            SumSpace = 0;
            len = str.Length;
            str.CopyTo(0, characterArray, 0, len);
            for (i = 0; i < str.Length; i++)
            {
                if (characterArray[i] == ' ') //Space
                    SumSpace++;
                else if (characterArray[i] == ' ')//?????
                    SumTab++;
                else ;
            }
        }
   }
}
Posted
Comments
Nish Nishant 18-Feb-11 12:55pm    
Hey Khalid,

I noticed you marked a post as answer, and then unmarked it. I don't mind the unmarking, but are you still stuck on this?

\t is tab.

C#
if (characterArray[i] == ' ')
  SumSpace++;
else if (characterArray[i] == '\t')
  SumTab++;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 11:46am    
Yes, yes... a 5,
--SA
fjdiewornncalwe 18-Feb-11 14:31pm    
Excellent. +5.
Espen Harlinn 19-Feb-11 12:52pm    
Nice and simple, my 5
C#
if (characterArray[i] == '\t')
   SumTab++;


Also, you might want to include the checks for '\n' (new line #10), '\r' (carriage return #13), '\b' (backspace, #8), Unicode characters in numeric forms, and more.

See: http://msdn.microsoft.com/en-us/library/aa691087(v=vs.71).aspx[^].

—SA
 
Share this answer
 
v2
You can also use linq:

C#
public int CharCount(char character, string myString)
{
    int count = (from c in myString 
                 where c == character
                 select c).Count();
    return count;
}


And call it like this:

C#
int spaceCount = CharCount(' ', "123 456");
int tabCount   = CharCount('\t', "123 456");
 
Share this answer
 
v2
Comments
Nish Nishant 18-Feb-11 11:40am    
In this case this will take double the time though, since you loop twice. But if you only want to count one particular character, then this works (easier to maintain).
#realJSOP 18-Feb-11 11:55am    
Code is more dynamic in its use, and when you get right down to it, the strings appear to be short. If it were me, I'd replace tabs with spaces, then replace multiple concurrent spaces with single spaces (accounting for stupid data entry clerks), and then count the spaces remaining. :)
Sergey Alexandrovich Kryukov 18-Feb-11 11:44am    
Good, my 5, even though Nishant is probably right.
--SA
In addition to Nishant and SA's answers, you can find here a list of all special characters (or escape sequences):
http://msdn.microsoft.com/en-us/library/h21280bw.aspx[^]
 
Share this answer
 
v2
You can also do it this way if you don't like (or can't use) LINQ:

C#
public int CharCount(char character, string myString)
{
    int count = 0;
    int pos = 0;
    do
    {
        pos = myString.IndexOf(character, pos);
        if (pos >= 0)
        {
            count++;
        }
    } while (pos >= 0);
    return count;
}
 
Share this answer
 
v3
Comments
Nish Nishant 18-Feb-11 12:13pm    
Missing while?
#realJSOP 18-Feb-11 12:15pm    
Oops. :) Fixed.
If you trying to find the number of spaces and tabs quickly, with no worry for optimization, another way to do is, utilize the exiting method of String.Split[^]

String.Split will return an array of strings with the split strings based on your criteria, then you need to count your array size and your are done.


str = "123       456";
string [] split = str.Split(new Char [] {' '});
spaces = split.Length - 1
 
Share this answer
 
Comments
#realJSOP 18-Feb-11 12:42pm    
That requires twice the memory.

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