Check if a string contains not defined characters [closed] - c#

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 8 years ago.
Improve this question
I have a pre defined string as Follows.
string preDefined="abc"; // or i can use char array in here
string value="ac";
string value1="abw";
I need some function to compare value with preDefined.
(value.SomefunctionContains(preDefined)
this function needs to return
value -> true;
value1 -> false
I knew that i can't use contains() or Any(). so plz help

You are just looking for if value has any character that is not in predefined, so this should do it:
!value.Any(x => !predefined.Contains(x))
Or it's more clear using All:
value.All(predefined.Contains);

private bool SomeFunction(string preDefined, string str)
{
foreach (char ch in str)
{
if (!preDefined.Contains(ch))
{
return false;
}
}
return true;
}

You can implement the following method to get the result :
private static bool DoesContain(string predefined, string value)
{
char[] c_pre = predefined.ToCharArray();
char[] c_val = value.ToCharArray();
char[] intersection = c_pre.Intersect(c_val).ToArray();
if (intersection.Length == c_val.Length) {
return true;
}
else {
return false;
}
}
Please not that this solution is a generalized implementation. IT also returns true even if the characters are not in the same order, unless ther include all.

Related

Convert an entire string value into ASCII number in ASP.NET Core [closed]

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 months ago.
Improve this question
I need to convert the entered string value into an ASCII value to store into database as an array object. It is required as per the functional requirement.
You can use the below code -
static void Main(string[] args)
{
string value = "Hello World";
Console.WriteLine(value);
int[] result = new int[value.Length];
int index = 0;
foreach (var c in value)
{
if (char.IsDigit(c))
{
result[index] = Convert.ToInt32(c);
}
else
{
result[index] = ((int)c);
}
index++;
}
foreach (int i in result)
{
Console.Write("{0} ", i);
}
}
The if part is added due to consider if the string value contains any numerical value. That's why it will check either that particular character is digit or not. If digit then it will cast the value into Integeer type. Otherwise, else block will be called for all the characters value.

C# Best practice (LINQ) for check a string in a List/Array of strings [closed]

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
Im trying to check if a string in on a list/array of string
i know the simplest way is :
string[] animals = {"cat", "Dog", "Lizard", "Goat", "Mouse", "Cow"};
string MyAnimal = "Dog";
bool myAnimalIsValid = false;
foreach (string animal in animals)
{
if (animal == MyAnimal // or animal.Contain(MyAnimal))
{
myAnimalIsValid = true;
}
}
if (myAnimalIsValid)
{
//my code
}
I know there is other way to do that like using Select() or Where()
Do you think there is a good optimized way to do that ?
The simplest approach, so without substring but full-string comparison:
bool myAnimalIsContained = animals.Contains(MyAnimal);
Case insensitive:
bool myAnimalIsContainedOgnoringCase
= animals.Contains(MyAnimal, StringComparer.OrdinalIgnoreCase);
But you want to check if any of the animal-names in the list contains your animal as substring?
Then you can use:
bool myAnimalIsContainedAsSubstring = animals.Any(a => a.Contains(MyAnimal));
Case insensitive:
bool myAnimalIsContainedAsSubstringIgnoringCase
= animals.Any(a => a.IndexOf(MyAnimal, StringComparison.OrdinalIgnoreCase) >=0);

Example Bruteforce Algorithm [closed]

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 am currently getting into Pentesting and Ethical Hacking to test website security.
I would appreciate an example Bruteforce algorithm that is stored in a string. Not a dictionary algorithm, but a bruteforce algorithm. For example, it tries the letter a. Then it tries the letter b, then it tries c and so on. Thank you in advance :)
Even if i think that you should really come up with an own concept for this problem, i'll like to give you a hint how to do this.
Disclaimer: this is the laziest, slowest and dirtiest approach possible but it gets its job done. NEVER EVER USE THIS FOR A REAL SYSTEM.
Programm.cs
class Program
{
static void Main(string[] args)
{
Brutforce b = new Brutforce()
{
Alphabet = new []{'a', 'b', 'c', 'd'}
};
ICollection<string> vals = b.Calculate(3);
foreach (var elem in vals)
Console.WriteLine(elem);
Console.ReadKey();
}
}
Brutforce.cs
internal class Brutforce
{
public ICollection<char> Alphabet { get; set; }
private ICollection<string> _calculate(int lenght)
{
if (lenght <= 1) return Alphabet.Select(a => a + "").ToList();
ICollection<string> sub = _calculate(lenght - 1);
return (from alpha in Alphabet from prior in sub select alpha + prior).ToList();
}
public ICollection<string> Calculate(int lenght)
{
return Alphabet == null ? null : _calculate(lenght);
}
}

C# checking a whole char for numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello i got a string that im converting to a char
and cheking if it has a digit but i got a bug when i type like
122Abssde it goes thru anny suggestions?
foreach (char cha in myString)
{
if(char.IsDigit(cha))
{
return Int32.Parse(MyString)
}
}
I feel like taking risk to answer that but you can use Enumerable.All with char.IsDigit method and parse it to int like;
if(myString.All(char.IsDigit))
{
return int.Parse(myString);
}
Or better, use int.TryParse to check your string is valid integer or not.
write a function
public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
Int32 result;
return Int32.TryParse(val, NumberStyle,
System.Globalization.CultureInfo.CurrentCulture, out result);
}
usage
var _isNumeric2 = isNumeric("9.", System.Globalization.NumberStyles.Integer);

How to properly use the IF Statement [closed]

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 9 years ago.
Improve this question
Which one is correct and WHY
in both examples we have a function that determines if a certain string is valid...
(using some other function that's not defined here)
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (Validator.Validate(foo))
{
return true;
}
else
{
return false;
}
}
in the second scenario we have a function that ends with a TRUE statement and with no else.
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (!Validator.Validate(foo))
{
return false;
}
return true;
}
NOW INB4 please dont say that you can simply do it this way
return Validator.Validate(foo);
How to save a few lines its not what i want to know...but the implications and unknown consecuences ( to me ) of using one method or the other.
Because both IsValid() methods do nothing else they are equivalent.
All 3 are correct. The third is my preference because it's less code and still very readable in this case.
I think that the best solution is:
public bool IsValid(String foo)
{
return (Validator.Validate(foo))? true : false;
}
In addition the conditional expression is easy to understand and it's inline

Categories