c# combine code with string - c#

i am wondering how to combine code + int/string
example.
string USERINPUT = Console.ReadLine();
Console.ForgroundColor = ConsoleColor.USERINPUT
but that does not work. how to i wonder?

For the assignment
Console.ForegroundColor = (something here);
you must assign a ConsoleColor, which is an enum.
You can parse an enum value from it's string equivalent.
Console.ForegroundColor =
(ConsoleColor)System.Enum.Parse(typeof(ConsoleColor), USERINPUT);
For details see:
Search for a string in Enum and return the Enum
Note that my code does not include error handling. If the user types in a string at the console that is not a member of ConsoleColor, you will get an error condition.

Related

String (not just letter) to ConsoleKey c#

So i need to convert string to ConsoleKey...
Yes something like this works:
string ch = "a";
ConsoleKey ck = (ConsoleKey)Convert.ToChar(ch);
But what if the string is like "UpArrow" (the string is from ReadKey input and saved to txt file)
Please help.
You can convert a string to an enum member using Enum.Parse or Enum.TryParse.
Unfortunately the API is not generic, so you have to specify the type a few times:
ConsoleKey key1 = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), "UpArrow");
The above will throw an exception if the string is not a member of the enum. To protect against that you could use:
if (Enum.TryParse("UpArrow", out ConsoleKey key2))
{
// use 'key2' in here
}

What do I use to convert string into unicode

I am having an issue with this block of code.
Console.WriteLine("What is your name?");
string PlayerName = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(PlayerName);
What I'm trying to do is get the computer to read the name you input, and ask you if that is your name. Directly after I type my name though there is an exception. Convert.ToInt32 isn't what I'm supposed to use, and I guess my question is what do I put there instead.
I am new to programming and I'm not even sure if it's called unicode. Sorry.
Console.ReadLine() will return a string, no need to do any conversions:
Console.WriteLine("What is your name?");
string PlayerName = Console.ReadLine();
Console.WriteLine(PlayerName);
Convert.ToInt32() throw casting error if you do not pass valid integer value inside it. So, you need to check it gracefully and get interger value. For that, you can Int32.TryParse(input, out val) and get integer value.
Ex :
int value;
if(Int32.TryParse(your_input, out value))
{
// if pass condition, then your input is integer and can use accordingly
}
So, your program will be like this if you want to print integer value only :
Console.WriteLine("What is your name?");
var value = Console.ReadLine();
int intVal;
if(Int32.TryParse(value, out intVal))
{
Console.WriteLine(intVal);
}
If you want only to print what you've got from ReadLine method, you can just have :
Console.WriteLine("What is your name?");
Console.WriteLine(Console.ReadLine());
Convert.ToInt32(String) "Converts the specified string representation of a number to an equivalent 32-bit signed integer". You are getting an error because you are not typing in an integer value in your console.
Your PlayerName variable is of type string, and the return value of Console.ReadLine() is already a string, so you don't need any conversion.
If you’re dealing with Unicode characters, you might have to set proper encoding like so
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
Console.WriteLine("What is your name?");
string PlayerName = Console.ReadLine();
Console.WriteLine(PlayerName);

Convert enum to string digit

I want to convert enum to string with three digits in C#.
Like this:
enum test : short
{
asdf, qwer, zxcv
}
test t = test.qwer;
string result = t.ToString("%03D") // Is this right?
I want to print 001
In a normal enumeration you can just cast as an integer, then call "ToString" with a format specified. In your case that should look like:
test t = test.qwer;
string result = ((int)t).ToString("000");
That should do it! Good luck.

How could I create a string with an object value in it?

This questions really isn't hard to understand. I have always wondered this and wondered if possible, how could it be done. Well this is sort what I'd like to do if possible:
int number = 50;
string text = "The number is %number";
Where I wrote %number in the string above, I would like the value of number to be inserted into the string, because the way I would usually go about doing something like this would be like:
int number = 50;
string text = "The number is " + number.ToString();
Which yes, the way I integrated number into the string above is an okay way of doing so, but I have always wondered, is it possible to do something such as the first example I wrote, where to put a value of an object into a string all you would have to do is write some type of character or string (used to reference the object), along with the name of the object into a string to get a result of a string with the value of the object in it? Or is there anything sort of like this you're able to do?
You would use string.Format:
string text = string.Format("The number is {0}", number);
Note that all objects have a ToString method, which means that all objects can be used as arguments to string.Format, however the default response from ToString is to return the full name of the type, which might not make much sense.
For instance, this:
public class Dummy
{
}
var d = new Dummy();
string text = string.Format("The dummy is {0}", d);
will assign something like the following value to text:
"The dummy is Your.Namespace.Dummy";
You have two options:
Override ToString and return something meaningful for your new type
Read off a property or something instead, e.g.:
string text = string.Format("The dummy is {0}", d.SomeProperty);
Also note that string.Format can take multiple arguments:
int a = 10;
int b = 20;
int c = a + b;
string text = string.Format("{0} + {1} = {2}", a, b, c);
There's a lot more to string.Format than what I have shown here, so click the link to the documentation (first line of this answer) to learn more.
You probably need to check Sting.Format
string text = String.Format("The number1 is {0},number2 is {1}", number1, number2);
It is also worth to check this discussion regarding When is it better to use String.Format vs string concatenation?
`

C#: Is there a way to search a string for a number without using regex?

Is there a way to check to see if a string contains any numeric digits in it without using regex? I was thinking of just splitting it into an array and running a search on that, but something tells me there is an easier way:
//pseudocode
string aString = "The number 4"
If (aString contains a number) Then enter validation loop
Else return to main
//output
"The string contains a number. Are you sure you want to continue?"
var containsdigit = somestring.Any(char.IsDigit);
You could use String.IndexOfAny as:
bool isNumeric = mystring.IndexOfAny("0123456789".ToCharArray()) > -1;
You could create an extension method for string and use a combination of LINQ and the Char.IsNumber function e.g.
public static class StringExt
{
public static bool ContainsNumber(this string str)
{
return str.Any(c => Char.IsNumber(c));
}
}
Then your logic would look like:
//pseudocodestring
string str = "The number 4";
If (aString.ContainsNumber())
enter validation

Categories

Resources