0

i need some help. I have to write a programm where you can do some calculation with matrices.

The User input should be for example: A=[1,2,3;4,5,6;7,8,9]

The user should be able to save up to 10 matrices. The user should be able to write operations like A+B or C*D

I want to check, if the first character of the users input is a letter, if not, i want to give an exception. Is there a method in c# where you can check if the first character is a letter. I want to save the letters into a string array so I can reference the name of the matrices to the int [,] which contains the matrices. Here is a snippet of my code:

int i = 0;
int[][,] ArrayContainer = new int[10][,];
int rowcount;
int columncount;

while (i < 10)
{
    string input = Console.ReadLine();
    string trimedinput; 

    if (input.Contains(" "))
    {
        trimedinput = input.Replace(" ", string.Empty);
    }
    else if (input == String.Empty)
    {
        break;
    }
    else if(!input.Contains("="))
    {
        Console.WriteLine("The definition of your matrix is not correct. Please     type in 'help' if you need help.");
        continue;
    }
    else
    {
        trimedinput = input;
    }
}

Thank you for help!

Tzah Mama
  • 1,547
  • 1
  • 13
  • 25
  • How do you define "letter" here? a-z/A-Z? what about unicode? Would a regex check of `\p{L}` work? `char.IsLetter` ? or just `(str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z')` ? – Marc Gravell Jun 10 '14 at 09:24

4 Answers4

3

you can use Char.IsLetter as shown below :-

for example :-

string str = " I am a string";
bool isLetter = !String.IsNullOrEmpty(str) && Char.IsLetter(str[0]);

For more information :-

http://msdn.microsoft.com/en-us/library/system.char.isletter.aspx

Neel
  • 11,625
  • 3
  • 43
  • 61
1

Use Char.IsLetter.

bool isLetter = Char.IsLetter(str[0]);
dcastro
  • 66,540
  • 21
  • 145
  • 155
1

You could use char.IsLetter():

string foo = "Hello world";
bool isLetter = char.IsLetter(foo, 0);
DGibbs
  • 14,316
  • 7
  • 44
  • 83
1

You could use the the method IsLetter of Char type.

For instance if you have a string called test and you want to check if it's first character is a letter, you could check it out like below:

bool isLetter = Char.IsLetter(test[0])

For further documenation, please have a look here.

Christos
  • 53,228
  • 8
  • 76
  • 108