NaveenBhat
NaveenBhat

Reputation: 3318

Tab Space on C Sharp

I have created a small program to write XML-Comment for Properties(which dont have customer attributes applied).

    /// <summary>
    /// Write XML-Comment for Properties(which dont have customer attributes applied).
    /// </summary>
    /// <param name="fileName">'*.cs' file along with full path to comment.</param>
    /// <returns></returns>
    static bool WriteComment(string fileName)
    {
        List<string> code = new List<string>();

        using (StreamReader sr = new StreamReader(fileName))
        {

            while (!sr.EndOfStream)
            {
                code.Add(sr.ReadLine());
            }

            List<int> indexes = (from line in code
                                 where line.Contains("get")
                                 select code.IndexOf(line)).ToList();

            int count = 0;
            foreach (int i in indexes)
            {
                code.Insert((i + count++) - 2, getWhiteSpace(code[(i + count) - 3]) + "/// <summary>");
                code.Insert((i + count++) - 2, getWhiteSpace(code[(i + count) - 3]) + "/// Gets or Sets the " + getPropertyName(code[(i + count) - 3]));
                code.Insert((i + count++) - 2, getWhiteSpace(code[(i + count) - 3]) + "/// </summary>");
            }
        }

#if DEBUG
        foreach (string line in code)
            Console.WriteLine(line);
#else
        using (StreamWriter sw = new StreamWriter(fileName))
        {
            foreach (string line in code)
                sw.WriteLine(line);
        }
#endif

        return true;
    }

    /// <summary>
    /// To retrive property name.
    /// </summary>
    /// <param name="property">Property Name Line(line with visibiliy mode and return type ).</param>
    /// <returns>Property Name.</returns>
    public static string getPropertyName(string property)
    {
        return Regex.Replace(property.Substring(property.LastIndexOf(" ")), "([a-z])([A-Z])", "$1 $2").TrimStart();
    }

    /// <summary>
    /// Get initial position of the property. So that XML-comment can be placed properly 
    /// at the top.
    /// </summary>
    /// <param name="codeLine">Property Line code.</param>
    /// <returns>Number of White Space.</returns>
    public static string getWhiteSpace(string codeLine)
    {
        string retStr = "";
        foreach (string s in codeLine.Split())
        {
            if (s != string.Empty) break;
            retStr += " ";
        }
        return retStr;
    }

As you can see on the method getWhiteSpace I'm checking for Empty String and it works well. But, if I apply string.Empty to retStr on loop...the returned value will be string.Empty only! This I can understand that concatenating the string.Empty will result string.Empty only.

Now my question is how if (s != string.Empty) is working and why if (s != " ") or if (s != "\t") is not working?

Upvotes: 0

Views: 2458

Answers (3)

yas4891
yas4891

Reputation: 4862

If you are on .NET 4.0, have a look at String.IsNullOrWhiteSpace()

if(s != string.Empty) break;
if(s != "") break;

are essentially the same. this should tell you why

if(s != " ") break;

is not working. It is because "" is not equal to " ".
the same is true for "\t": it is not equal to either " " or ""


Update: I figure you're trying to get the number of whitespaces at the beginning of each line in order to correctly indent the auto generated comments. I'd probably go about this as follows:

EDIT: removed own code. Instead take a look here:

Get leading whitespace

Upvotes: 1

EOG
EOG

Reputation: 1747

String.Empty is "" If you want to check if string is "\t" or " " use string.IsNullOrEmpty and string.IsNullOrWhiteSpace methods

Upvotes: 2

Zenwalker
Zenwalker

Reputation: 1919

\t is for a tab space. Tab space is different from empty space. Tab space will have more than 1 empty space in it.

String.Empty and "" is same, except that 1 deciphers at run time and other at compile time.

Upvotes: 0

Related Questions