How to check a string for a question mark? [duplicate] - c#

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.

Related

C# Add conditional directly inside string [duplicate]

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.

reuse interpolated strings [duplicate]

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.

How to validate negative string number [duplicate]

This question already has answers here:
How can I check if a string is a number?
(25 answers)
Closed 7 years ago.
i'm currently doing a C# WPf application.
May i know how to validate for the string value? Example "0.00" or "-1.00",etc?
Because both of them is return as a string that i retrieve from SAP, is there anyway to check?
Sorry I'm still new to C# , not really familiar with the function that have in the C#.
I assume you want to check if the number that you get back is a negative number?
double number = 0;
if(double.TryParse(myString,out number)){
if (number > 0)
\\Do something
}
Do you always have a double value?
Try
Double.Parse(...)
edit: corrected method
you can use TryParse() as bellow
string s="0.00";
double dvalue=0;
if(!double.TryParse(s,out dvalue)){
//not valid
}

Difference between bool/Boolean, string/String etc [duplicate]

This question already exists:
Closed 12 years ago.
Possible Duplicate:
String vs string in C#
What is the big difference between these datatypes? and were should I use it?
Short answer, there is no difference. They are just alias of each other, see
http://msdn.microsoft.com/en-us/library/ya5y69ds(VS.80).aspx for a complete list.
Have a look at: String vs string in C#

Datatype String vs string in C# [duplicate]

This question already exists:
Closed 13 years ago.
When to use "String" and when to go for "string" data type in C# ?
What is the difference between them?
Thanks.
Duplicate: String vs string in C#
There is no difference. string is an alias of String. The same goes for int and Int32 etc.
They are the same. Choose one notation and stick to it.

Categories

Resources