1

I am programming a thing where I am asking a user to type in several ingridients, how much they want of it and what the kg/ml/liter/etc price is. So that I add all the cost values(after multiplying the cost per unit times the measure they want) later display for them the most cheap and expensive ones. This is done in Visual Studio and the language is in C#. So I am typing this:

        static void Ingridiens()
        {
            string ingridiensen;
            Console.Write("Vad för ingridiIngridiensens behöver du?\nIngridiens: ");
            ingridiensen = Console.ReadLine();
            listaformat.Add(ingridiensen);
            PrisPerEnhetEtt(prisetPerEnhet);
        }

Ignore the variable names since they are in swedish. What I want help in is to check whether the input of the user is letters or something else. If they are not letters like numbers or any other special characters I want to return an error. And I also want to check if they are typinh one letter or not. It is one letter I still wanna return an error. But if they both type in letters(i.e. at least 2 letters) than I want to move on to the next method which in my case is PrisPerEnhetEtt.

I am finding it hard to fix this. I tried a lot of stuff, if statements, switch statements but i seems i need to invoke boolean variables. I am not sure how to do it. I am quite new to programming.

Thanks for all the help! :D

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Arsen
  • 17
  • 1
  • 2
  • [`Char.IsLetter`](https://learn.microsoft.com/en-us/dotnet/api/system.char.isletter?view=net-5.0) can possibly suit your needs. – Guru Stron Jan 05 '21 at 12:17

1 Answers1

1

Using foreach

You can parse the chars of the string and use char.IsLetter and char.IsNumber or char.IsDigit like that with or without extension methods:

static public class StringHelper
{

  static public bool IsText(this string input)
  {
    foreach ( char c in input )
      if ( c != ' ' && !char.IsLetter(c) )
        return false;
    return true;
  }

  static public bool IsNumber(this string input)
  {
    foreach ( char c in input )
      if ( !char.IsNumber(c) )
        return false;
    return true;
  }

}

Difference between Char.IsDigit() and Char.IsNumber() in C#

Using Linq

using System.Linq;

static public bool IsText(this string input)
{
  return input.All(c => c == ' ' || char.IsLetter(c));
}

static public bool IsNumber(this string input)
{
  return input.All(c => char.IsNumber(c));
}

Or:

static public bool IsText(this string input)
  => input.All(c => c == ' ' || char.IsLetter(c));

static public bool IsNumber(this string input)
  => input.All(char.IsNumber);

Linq can be as slow as it is faster than loops depending on the processing done on the nature and the complexity and the amount of data. But Linq provides simple, clean, small, maintainable, robust, and magical code once learned.

For vs. Linq - Performance vs. Future

Is a LINQ statement faster than a 'foreach' loop?

Is the LINQ version faster than the foreach one?

Usage

string ingridiensen;
Console.Write("Vad för ingridiIngridiensens behöver du?\nIngridiens: ");
ingridiensen = Console.ReadLine();

Console.WriteLine(ingridiensen.IsNumber());
Console.WriteLine(ingridiensen.IsText());

Remark

In fact we can write to check if integer or double:

return int.TryParse(input, out var _);

return double.TryParse(input, out var _);

Advanced conditions

You can adapt the test conditions and create as many methods to match your needs : lower or upper case, space or no space allowed, point, special chars, integers, decimals, and so on:

static public bool IsWhatYouNeed(this string input)
{
  foreach ( char c in input )
    if ( !match(c) )
      return false;
  return true;
  void bool match(char c)
  {
    ...
  }
}

You can also use the char position if needed:

static public bool IsWhatYouNeed(this string input)
{
  for ( int index = 0; index < input.Length; index++ )
    if ( !match(input[index], index) )
      return false;
  return true;
  void bool match(char c, int pos)
  {
    ...
  }
}

The code above is lousy but written to give you the idea if needed.

  • 2
    In `input.ToCharArray().All(...)` the char array is not needed since `string` is already an `IEnumerable`, so that can be replaced with just `input.All(...)`. Also, it could be shortened to `input.All(char.IsNumber)` – Ghost4Man Jan 05 '21 at 12:51