This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.
Related
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
operators as strings
(13 answers)
Closed 3 years ago.
I have
i = double.Parse(TextBox.Text);
but when I enter the + symbol, this error appear "System.FormatException: 'Input string was not in a correct format.'"
If I understand correctly, you have a much larger issue than you think.
Your textbox has a string. In this case, "1+1" is the value in your textbox. However, that cannot be parsed to an integer value because it contains the plus sign. The plus sign is a character, it is not an integer (0,1,2,3,4..). So, what you get is a data type conversion conflict.
From what I gather, you'd like to evaluate that expression and then store the value into into the i variable. In this case, you would like i to equal 2.
You will need to evaluate the string and convert it into a formula then use the result to store in the variable.
Here's a link to an example of a conversion formula.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f62b87d-a35c-4074-a0f0-84a9dd7ff0a5/convert-string-to-formula?forum=csharpgeneral
This question already has answers here:
How to write Unicode characters to the console?
(5 answers)
Closed 4 years ago.
i want to ask if is possible to add in a string the "⚠" character and make it executable in console with Console.WriteLine().
you can use this code;
System.Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine("⚠");
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
I want to check if an inputted string contains a question mark.
Probably quite simple but I'm new to coding.
Use String.Contains() :
string myString = "Hello world?";
bool containsQuestionMark = myString.Contains("?"); // true
For future references, use MSDN, it's filled with good documentation.
Alternatively (to Rick's answer), if you are checking just for char occurance in a string you can use IndexOf(char):
bool containsQuestionMark = myString.IndexOf('?') != -1;
There are also some minor (negligible) performance differences between two approaches, depending on the framework version being used.
This question already has answers here:
c# string character replace
(4 answers)
Closed 7 years ago.
I'm having problem in assigning a value to the specific index in the string after checking on the same index:
Here below is my code:
"bits" is a string and "dirtybit" is an integer.
if (bits.ElementAt(dirtybit).Equals('1'))
bits[dirtybit] = '0'; //shows red underlined error
Error:
Property or Indexer String.this[int] cannot be assigned to -- is only read
Why can I not access the same index (value)?
Is there any workaround?
Strings are immutable in C#. You can not change them after you have created them.
You can use StringBuilder to create a new string.
From MSDN:
Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this.
This question already has answers here:
String Interpolation with format variable
(7 answers)
Closed 7 years ago.
Has anyone figured out how to reuse an interpolated string?
I.e. can anyone figure out how to do away with the string.Format in the following block of code?
foreach(var s in new[]{ "Primary_{0}_Home", "Secondary_{0}_Work" }){
sql = $"SELECT {string.format(s, "Street")}, {string.format(s, "City")} ..."
}
You can't do that.
Interpolated strings are set on compile time. You can't use string interpolation to load a string to format something not directly in the scope.