Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Console.WriteLine("Would you like to go first or second?");
string firstSecond = Console.ReadLine().ToUpper();
if (firstSecond == "FIRST")
{
Console.WriteLine("1");
}
else if (firstSecond == "SECOND")
{
Console.WriteLine("2");
}
else
{
string input = "x";
bool first = input == "FIRST";
bool second = input == "SECOND";
do
{
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
}
while (!(first | second));
}
The do while loop keeps on going even if I type first or second, I tried changing how I state the boolean but nothing seems to work. I am new so I am might some silly mistakes. How do I fix this?
You must put first and second condition into do while loop.
string input;
bool first;
bool second;
do {
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
first = input == "FIRST";
second = input == "SECOND";
}
while (!(first || second));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The while loop
public static bool Authentication()
{
bool isAuthenticated = false;
int count = 4;
while (isAuthenticated = false && count > 0)
{
Console.WriteLine("Please insert your 4 digit PIN");
string pin = Console.ReadLine();
if (pin == "1704")
{
isAuthenticated = true;
return isAuthenticated;
}
else
{
count--;
Console.WriteLine($"Wrong PIN: {count} Input remained ");
}
}
return isAuthenticated;
}
I haven't been able to understand, when the debugger goes to the while loop, it does not execute it. It may be the condition of the while that does not fit the logic.
I believe you are using the = as an assigning operator in the condition of the loop. You could try !isAuthenticated instead of isAuthenticated == false and see if that works
while (!isAuthenticate && count > 0)
I believe you were assigning false to isAuthenticate, so the condition could never be true.
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 2 years ago.
Improve this question
I was wondering if anyone could please tell me a nicer way of writing this. These are the only conditions that are used.
private static void BondedNonBoundedIndicator(InvoiceLine invoiceLine, Product packageProduct)
{
var value = packageProduct.BondedQuantity;
var value2 = packageProduct.NonBondedQuantity;
if (value == 0 && value2 == 1)
{
invoiceLine.BondedORBbondedIndicator = "N";
}
if (value == 1 && value2 == 0)
{
invoiceLine.BondedORBbondedIndicator = "Y";
}
}
Using C# 8, we can use tuples and the switch expression to simplify this:
invoiceLine.BondedORBbondedIndicator = (value, value2) switch {
(0, 1) => "N",
(1, 0) => "Y",
_ => invoiceLine.BondedORBbondedIndicator
};
You could shorten the last case even further if you know the invoiceLine.BondedORBbondedIndicator before the code is run is always, say ""(, then the last case would be _ => "").
You can use xor logic
If(value1^value2 == 1)
value1 == 0 ? invoiceLine.BondedORBbondedIndicator = "N" : invoiceLine.BondedORBbondedIndicator = "Y";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i'm developing a C# app.
I have a text input field with a submit button.
When I click on the button I want to check if the text is excactly: "CODxxxxx" (x=number).
Actually I have this:
if (inputText1.ToLower().Contains("COD") && inputText1.Length.ToString() == "8")
{
//DO THINGS
}
but if the string is xxxCODxx the statement returns true.
I think regular expressions can help me, but I never used with C#...
Try regular expression:
bool correct = Regex.IsMatch(inputText1, "^COD[0-9]{5}$");
If you want to ignore case (i.e. Cod, cOD etc. are supposed to be correct), just add option:
bool correct = Regex.IsMatch(inputText1, "^COD[0-9]{5}$", RegexOptions.IgnoreCase);
You can also solve it without RegEx
string input = "COD12345";
bool result = input.StartsWith("COD") && input.Skip(3).All(x => char.IsDigit(x)) && input.Length == 8;
Try:
inputText1.ToLower().StartsWith("cod") && inputText1.Length == 8
Or if you realy want it with regex,
this will be the right pattern:
Regex.IsMatch( inputText1, "COD\w{5}");
You can either use regular expressions:
Regex.IsMatch( text, "^COD\\d{5}$");
Or your own Solution:
string txt = inputText1.ToLower();
if (txt.StartsWith("cod") && txt.Length == "8")
{
int a = 0;
int.TryParse(txt.Substring(3), out a);
if(a!=0){
//DO THINGS
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to pass if input string is "A" means i need to get next leve as "B". If input value is "B" means "C".
E.g:
string input1 = "A";
string input2 = "B";
output:
input1 ---> B
input2 ---> C
How to do it in programmatically. Please give me a solution to do this.
This is the best as far as i understand the problem:
public char NextLevel(char c)
{
if (c == 'Z')
return 'A';
return (char)(c + 1);
}
private string NextChar(string character)
{
if (character == null) throw new ArgumentNullException(nameof(character));
if (character.Length != 1) throw new ArgumentOutOfRangeException(nameof(character), "You can use only a single letter string");
return Convert.ToString(NextChar(character[0]));
}
private char NextChar(char character)
{
return (char)(character + 1);
}
If you need more details you can use this link: https://github.com/jenseralmeida/stackoverflow/blob/q37608162/src/LibraryTests/DraftTests.cs
This should work for you.
public string NextAlphaString(string x)
{
if(x == "z" || x == "Z")
return x.Replace(x[0],(char)(x[0] - 25));
else
return x.Replace(x()[0],(char)(x[0] + 1));
}