is there anything special about the word "is" in C#?

Hello forum goers, I’m making a karaoke-like text highlighter and for some reason it always chokes on the word “is”. Any other word will highlight correctly so far but the word “is” will be skipped for no apparent reason no matter what part of a sentence it appears in. If the letters “is” appear in a word such as “this” it’s fine or if I capitalize one of the letters in it it also works. I’m splitting the string on spaces, comas and periods so the word “is” is being put into it’s own string if that’s important in some way. I haven’t seen anything in my google search to suggest that this word should cause problems but I might just be using the wrong search string to find the results I’m looking for. Anyone more familiar with C# have an idea why this could happen?

It’s a reserved word, but should be fine in a text string (unless you’re using something like eval on said string).

More on ‘is’:

http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

Check out:
http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

“is” is a c# keyword

*edit: as said above, should be fine as part of a string i.e string str = “this is a string”;

How are you using it? “is” is a keyword in C#, but only as part of compiled code, not in the context of a string. (you should be able to have anything in your string without it causing an error). Are you trying to execute some string or something?

Essentially “is” lets you check types:

string test = "asdf";
int anotherTest = 5;
object aComplexType = new Dictionary<string, bool>();


if (test is string)
{
}

if (anotherTest is System.Int32)
{
}

if (aComplexType is Dictionary<string, bool>)
{
}


class Foo : Bar
{
}
class Bar
{
}

Foo foo = new Foo();

if (foo is Bar)
{
}

EDIT: DAMN YOU TWO!

And In UnityScript ? :slight_smile:

Kukuku…the fat chicken laughs

UnityScript might have the same behaviour. Or it might be “instanceof” instead. Who ever knows with UnityScript? :smile:

And a big kuku tu u tu.

I quickly wrote a simplified version of what I’m doing to show (there may be some things missing, I’ll take another look at it in a bit):

public class MyClass 
{
	private string mPageText;
	private float mHighlightWords = 0f;
	private string[] delimitedArr;
	private WordData[] mWordData;
	private string mHighlightString;

	public MyClass()
	{
		mPageText =  "This is our little friend.";

		char[] delimiterChars = { ' ', ',', '.', '\n' };
	
		delimitedArr = mPageText.Split(delimiterChars);

		// make word datas~
		mWordData = new WordData[delimitedArr.Length];
		for(int i=0;i<delimitedArr.Length;i++)
		{
			mWordData[i] = new WordData(delimitedArr[i]);
		}
	}

	public string GetHighlightString(int index)
	{
		string highlightString;
		int tmpIndex = index;
		
		if(index >= mWordData.Length)
		{
			tmpIndex = mWordData.Length-1;
		}
		else
		{
			tmpIndex = index;
		}
		
		WordData wordData = mWordData[tmpIndex];
		int endIndex = wordData.startIndex+wordData.numCharacters;
		
		highlightString = mPageText.Substring(0, endIndex);
		
		return highlightString;
	}

	void FixedUpdate()
	{
		mHighlightWords+= 0.02f;
		
		int highlightWords= Mathf.FloorToInt(mHighlightWords);

		mHighlightString = GetHighlightString(highlightWords);
	}
	
	void OnGUI()
	{
		GUI.Box(new Rect(mBookDisplayX + pageText.pageLocation[i].x,mBookDisplayY + 
			pageText.pageLocation[i].y, 500, 100),mPageText, mBookWordStyle);
	
		GUI.Box(new Rect(mBookDisplayX + pageText.pageLocation[i].x,mBookDisplayY + 
			pageText.pageLocation[i].y, 500, 100),mHighlightString, 
			mBookWordHighlightStyle);
	}
}

public class WordData
{
	private string mWord;
	private int mStartIndex;
	private int mNumCharacters;
	
	public string word
	{
		get	{return this.mWord;}
	}
	
	public int startIndex
	{
		get	{return this.mStartIndex;}
		set	{this.mStartIndex = value;}
	}
	
	public int numCharacters
	{
		get {return this.mNumCharacters;}
	}
	
	public WordData(string passedWord)
	{
		mWord = passedWord;
		mNumCharacters = passedWord.Length;
	}
}

EDIT: I already knew about “is” being a keyword by the way, I just didn’t think it should affect anything while it was inside a string.

You were correct; C# definitely doesn’t have any problem with the two-character sequence [‘i’, ‘s’]. C# doesn’t care what is inside your strings. Whatever the problem is, it’s not that.