This question already has answers here:
How can I get a character in a string by index?
(2 answers)
Closed 5 years ago.
string qwe = "ABCD";
if(qwe.StartsWith("A") && qwe.EndsWith("D"))
{
MessageBox.Show("Message");
}
What I need is to make also a decision for B and C but not StartsWith and EndsWith, it's really hard for me to explain but just like this:
if(qwe.Second("B"))
{
//Do anything
}
and
if(qwe.Third("C"))
{
//Do anything
}
You know that you can access characters via index(zero based)?
if(qwe.Length >= 2 && qwe[1] == 'B')
{
//Do anything
}
if(qwe.Length >= 3 && qwe[2] == 'C')
{
//Do anything
}
Related
This question already has answers here:
Can you explain lambda expressions? [duplicate]
(6 answers)
What does the '=>' syntax in C# mean?
(7 answers)
Closed 4 years ago.
I am new to C# and coding in general and have a question on one exercise I'm doing. I am following the exercises on w3resource and I came across a challenge where I have to solve this:
"Write a C# program to check if a given string contains 'w' character between 1 and 3 times."
My solution was this:
var theString = Console.ReadLine();
var theArray = theString.ToCharArray();
int betweenOneAndThree = 0;
for (int i = 0; i < theArray.Length - 1; i++)
{
if (theArray[i] == 'w')
betweenOneAndThree++;
}
Console.WriteLine(betweenOneAndThree >= 1 && betweenOneAndThree <= 3);
Console.ReadLine();
This worked just fine, but I checked their solution and it goes like this:
Console.Write("Input a string (contains at least one 'w' char) : ");
string str = Console.ReadLine();
var count = str.Count(s => s == 'w');
Console.WriteLine("Test the string contains 'w' character between 1 and 3 times: ");
Console.WriteLine(count >= 1 && count <= 3);
Console.ReadLine();
I can't see 's' being declared as a char variable and I do not understand what it does here. Can anyone please explain to me what s => s == 'w' does?
Yes, I have tried googling this. But I can't seem to find an answer.
Thank you in advance :)
This is a lambda expression.
In this case it declares an anonymous delegate which is passed to Count, whose signature for this overload specifies a Func<T, bool> which is a typed representation of an anonymous function which takes a T (the type of the object in the collection) and returns bool. Count() here will execute this function against each object in the collection, and count how many times it returned true.
str.Count(s => s == 'w') is basically a shortened way to say this:
result = 0;
foreach (char s in str)
{
if (s == 'w')
{
result += 1;
}
}
return result;
s => s == 'w' is Predicate delegate with lambda expression,
str.Count(s => s == 'w') simply counts the occurences of the characters w
This question already has answers here:
Check whether an array is a subset of another
(10 answers)
Closed 8 years ago.
I create a list<string> machineTypes and fill it with data. I want to check to see if the collection contains any combination of strings. My initial plan was to use a for loop, but obviously I can't check multiple indexes in the middle of a for loop.
for (int i = 0; i < machineTypes.Count; i++)
{
if (machineTypes[i] == "W")
//do stuff
if ((machineTypes[i] == "P") && (machineTypes[i] == "W") && (machineTypes[i] == "A") && (machineTypes[i] == "C"))
//do stuff
}
So I'm looking for suggestions as to the best way to do this. I suppose I could use String.Join, but I was wondering if there was a more elegant way.
Perhaps not too elegant - but something like this might help
for (int i = 0; i < machineTypes.Count; i++)
{
int jj=i;
if (machineTypes[i] == "W")
//do stuff
if (jj< machineTypes.Count-4)
if ((machineTypes[jj] == "P") && (machineTypes[jj+1] == "W") && (machineTypes[jj+2] == "A") && (machineTypes[jj+3] == "C"))
//do stuff
}
I just made a new var jj in case you want to increment or alter its value, without altering the i value. Note the if (jj< machineTypes.Count-4) condition checking to see that you can safely use jj+3 as an index.
This question already has answers here:
Equivalent of Visual Basic's And and Or in C#?
(6 answers)
Closed 8 years ago.
I have the following code:
if (bl != closeButtonLabel)
{
if (bl != minimiseButtonLabel)
{
optionPanel.Controls.Remove(bl);
}
}
Is there a way to do this in 1 if but check the 2 conditions?
At VB it's easy, you place 'Or' and not 'OrElse' but in c# there is only '||'.
Can anyone help me?
You could try the logical AND operator, '&&'.
if (bl != closeButtonLabel && bl != minimiseButtonLabel)
optionPanel.Controls.Remove(bl);
You don't need the logical Or operator here, ||.
Let's say out loud, what you want: bl should be differnt fom closeButtonLabel AND it shoud be different from minimisButtonlabel
if (bl != closeButtonLabel && bl != minimiseButtonLabel) {...}
or if you really want to use OR
if (!(bl == closeButtonLabel || bl == mimimizeButtonLabel)) {...}
(DeMorgan's Laws)
if(bl != closeButtonLabel && bl != minimiseButtonLabel)
{
//do work
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a difference between using nested if statements and using one long if statement where there are a lot of arguments?
e.g.
if ( var1 == var2 && var3 == var4 )
{
//do something
}
or
if ( var1 == var2 )
{
if ( var3 == var4 )
{
//do something
}
}
I would like to know if there is any difference in doing things this way?
thanks
In general, the compiler will generate the same or nearly the same code for these two variants. For interpreted languages that support short-circuiting (not evaluating second part of an || or && when the outcome has been determined), it will also be pretty much the same thing.
My advice would be to use the form that represents what you want to do most clearly.
For example, if we want to check if two coordinates match:
if (x1 == x2 && y1 == y2)
Buf if your code is something like this:
if (country_of_origin == "Sweden")
{
if (today == thursday)
{
...
}
}
may make more sense...
The second style allows for checking that needs to be done independently of the first if condition.
For example, if you needed to do something in an extra condition where (var3 != var4) BUT (var1 == var2) still, it would be impossible to do with:
"if(var1 == var2 && var3 == var4)"
Thus, you would need something like:
if(var1 == var2){
if(var3 == var4){
}
//more code...
//... var3 & var 4 change...
//
if(var3 != var4){
}
}
As the commenter #Solace said, it simply allows for more flexibility.
They both do the same thing , but personally I think nested is neater and it also allows code to be executed on either side of the nested if, which is usually required
if ( var1 == var2 )
{
//do something here based on the first if but before second if
if ( var3 == var4 )
{
//do something
}
//do something here also which may be different based on the nested if result
}
But if you dont need this , then your first code is probably a tiny bit faster with just a single if
Different situations require different solutions:
if ( width == height && width == depth )
{
// Its a cube!
}
if ( x == y )
{
if ( p == q )
{
//do something for when x == y but p == q
} else {
//do something for when x == y but p != q
}
You cannot generalise for what constructs are should or should not be used - they are all appropriate in some situation.
Well, it depends what is your condition. If it's same as yours, than there is absolutely no need to choose the second way.
It's about writing clean and readable code - doing this like if (condition1 && condition2) you are suggesting that both conditions are absolutely necessary to the following fragment of code.
This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 9 years ago.
I have this if else statement which is assigned to compare results from a text box to a list of context. I am wondering how do i make it such that it is case insensitive ?
value = textbox1.Text;
if (article.contains(value))
{
label = qwerty;
}
else
{
break;
{
try this
if(article.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
{
// ....
}
else
{
// .....
}