Hi Guys
NP78 IsWhiteSpace
What is meant by IsWhiteSpace?. Why when 5 output is True and when 6 out put is False. Anyone knows please explain.
Thank you
using System;
class MyDataTypes
{
public static int
{
Console.WriteLine("-> char.IsWhiteSpace('Hello There', 5): {0}",
char.IsWhiteSpace("Hello There", 5));
Console.WriteLine("-> char.IsWhiteSpace('Hello There', 6): {0}",
char.IsWhiteSpace("Hello There", 6));
return 0;
}
}
/*
-> char.IsWhiteSpace('Hello There', 5): True
-> char.IsWhiteSpace('Hello There', 6): False
*/
Posted Feb 5, 2008, 2:58 PM
Thank you for your help, Alan & Kapil Deo Malhotra.
AlanPosted Feb 5, 2008, 8:16 AM
Consider this version of the program:
using System;
class MyDataTypes
{
public static int Main(string[] args)
{
Console.WriteLine("-> Utility.IsWhiteSpace('Hello There', 5): {0}",
Utility.IsWhiteSpace("Hello There", 5));
Utility u = new Utility();
Console.WriteLine("-> u.IsntWhiteSpace('Hello There', 6): {0}",
u.IsntWhiteSpace("Hello There", 6));
return 0;
}
}
public class Utility
{
public static bool IsWhiteSpace(string s, int index)
{
if (Char.IsWhiteSpace(s, index))
return true;
return false;
}
public bool IsntWhiteSpace(string s, int index)
{
return !IsWhiteSpace(s, index);
}
}
Here, IsWhiteSpace() is a static method of the Utility class and IsntWhiteSpace() is an instance method of the class.
So, when IsWhiteSpace() is called from code within the MyDataTypes class, you don't need to create an instance of the Utility class and can simply call the method by preceding it with the class name viz: Utility.IsWhiteSpace(s, index).
However, when IsntWhiteSpace() is called from the MyDataTypes class, you do need to create an instance of the Utility class (assigned here to the variable 'u') and then call the method through that instance viz: u.IsntWhiteSpace(s, index).
Notice that IsntWhiteSpace() calls IsWhiteSpace() internally but here you don't need to precede it with the class name because it's in the same class.
Posted Feb 5, 2008, 5:41 AM
It is much appreciated if you could demonstrate with example.
Kapil Deo MalhotraPosted Feb 4, 2008, 11:44 AM
Posted Feb 4, 2008, 9:44 AM
So True or False is decided by the sort of numerical value (eg: in this case 5 & 6).
Also please explain why it is called static method (char.IsWhiteSpace()).
AlanPosted Feb 4, 2008, 6:55 AM
The static method char.IsWhiteSpace() determines whether a character at a particular index within a string is classified as 'white space' or not.
White space includes the space character itself (' '), tab ('\t'), linefeed ('\n'), carriage return ('\r') and various other unicode characters. See this link for a full list:
http://msdn2.microsoft.com/en-us/library/1x308yk8(VS.80).aspx
So, in the example program, the character at index 5 in the string "Hello There" is a space (and hence white space) but the character at index 6 is a 'T' and therefore not white space.