I'm trying to make a list in string to get the usernames like this:
string[] whitelisted = { "example", "exampøle222", "example245"};
if (sentRequest.Sender.Username.ToLower().Contains(whitelisted))
You are checking if the username contains the list. You should be checking that the list contains the username.
whitelisted.Contains(sentRequest.Sender.Username.ToLower())
Your comparison is backwards. You need to see whether the array contains the string .
You probably want to check that the sentRequest.Sender.Username.ToLower() is present in the whitelisted list, right?
Then make it inverted if (whitelisted.Contains(sentRequest.Sender.Username.ToLower()))
The error you get says that the method you are calling i.e. string.Contains() can accept a string but cannot accept an array string[].
It can be done quite simply using the LINQ aggregate function.
string[] strings = {"example1","example2"};
string result = strings.Aggrigate((l,r)=>l+r);
You could also use the String.Join method
string[] strings = {"example1","example2"};
string result = String.Join("",strings);
However when I look at the rest of your code I would recommend solving the problem something like this.
if(whitelisted.IndexOf(sentRequest.Sender.Username.ToLower()) != -1)
That if statement will only be executed if the Username is in the white list.
Related
I have the following field that is calling the database Phone_Number. I would like to remove the 1- when the number is displayed.
So instead of displaying 1-###-###-####, I would like to display ###-###-####.
I tried the following:
string x= Phone_Number;
x.Remove(0,1);
Response.Write(x);
However, it keeps displaying 1-###-###-####.
What am I doing wrong?
Strings are immutable in C# - String.Remove call does not modify original string. It creates the new string in which specified characters are deleted and returns it. You should display result of this method call instead:
Response.Write(x.Remove(0,2)); // you should remove 2 characters
Or
Response.Write(x.Substring(2));
You need to set the result to x. strings are immutable in C#:
x = x.Remove(0,1)
Another method would be:
if (x.StartsWith("1-")
x = x.Remove(0,2);
This has the benefit of doing nothing if you get a phone number without the leading 1-.
Thanks to commenter for pointing out my error.
As you see there are too many ways to remove substrings from strings. A new way that you can also use is a Regular Expression just in case the value you want to remove have a complex pattern in the future.
var x = phoneNumber;
var result = Regex.Match(x, #"^(1-)?(.*)$").Groups[2].Value;
What is the best practice way to use two clauses in LINQ Contains method..
Title is string
This is my If statement :
if (oWeb.ParentWeb.Title.Contains("Ricky") || oWeb.ParentWeb.Title.Contains("John"))
I need solution like this :
if (oWeb.ParentWeb.Title.Contains("Ricky", "John"))
Since Title is string this actually has nothing to do with LINQ as the used Contains method is an instance method of string.
Assuming you have more strings to check, you can do something like that:
var strings = new[] {"Ricky", "John"};
if (strings.Any(oWeb.ParentWeb.Title.Contains))
// do something
But roryap's answer using a regex seems preferable (as long as the number of strings to check is not too big).
I don't think LINQ is the best option for that. Why not use regular expressions?
Regex.IsMatch(oWeb.ParentWeb.Title, "ricky|john", RegexOptions.IgnoreCase);
Contains takes only one parameter, so you cannot do it this way.
You can make an array of items, and check containment with it:
var titles = new[] {"Ricky", "John"};
if (titles.Any(t => oWeb.ParentWeb.Title.Contains(t))) {
...
}
I have the following line of code:
List<int> _people = code.data.loadScalarDataPoint(code.enums.pathNames.Department, "Department_Staff", RecId).Split('|').Select(int.Parse).ToList();
The static method loadScalarDataPoint returns a string of the selected column for given inputs.
In this instance, it returns a list of pipe delimited integers (e.g. 12|45|88|1543|123) or if the column is NULL it will return an empty string.
Using the linq Select(int.Parse) works if there is a result but without it throws the following error "Input string was not in a correct format"
I know why, as you can't parse an empty string into a int, however is there a way within the single line of code to check for this?
Or do I need to get the result into a string, check if it has contents and if so parse into a list of ints?
EDIT with full explanation: In your failing case, the loadScalarDataPoint method returns an empty string, which the call to Split(',') returns an IEnumerable<String> with one empty string in it. The call to Select(Int32.Parse) throws an exception because an empty string is not in the correct format.
Use
.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries)
why not check for non empty result first?
List<int> _people = code.data.loadScalarDataPoint(code.enums.pathNames.Department, "Department_Staff", RecId).Split('|').Where(a => a.Any()).Select(int.Parse).ToList();
Add the following before the select:
.Where(x=>!string.IsNullOrEmpty(x))
This basically ensures that when doing the select there are only elements that have a string value.
Be aware you will need to handle the empty case.
I'm trying to use Linq to determine if a string is NOT in an array. The code I'm using is:
if (!stringArray.Any(soughtString.Contains)){
doStuff();}
but it's not working. Obviously creating a foreach loop would suffice, but I'd like to understand why this line isn't working. And yes, the file has using System.Linq;
You're not asking if the string is not in the array, you're asking if none of the strings in the array are sub-strings in some other string. Apparently at least one is, even though it's not equal.
You just want to do a simple Contains check:
if(!stringArray.Contains(soughtString))
You are currently passing the "Any" function the "Contains" method (which is then being passed each string in the array). In other, words:
array.Any(s => soughtString.Contains(s));
Likely, you want it the other way:
array.Any(s => s.Contains(soughtString));
Is there a quick way to pack an array of strings to a string?
More specifically,I have an array of strings like this:
string[] Operators = {"+","-","x","/"} and I would like to pack it to
string sOperators = "+-x/"
Of course, the obvious way is to read each item in the array and put it in the string individually, but is there a better way that people smarter than me can think of?
I have tried:
string sOperators="";
String.Join(sOperators,Operators);
Unfortunately, that won't work for me. Any thought?
Your code sample could just be incomplete but based on you've posted the problem is that you're not assigning the joined string anywhere. I think the following will do what you want;
string joined = String.Join(sOperators, Operators);
Join returns a new string, it does not make any changes to the arguments you pass it. You need to assign the return value to some field, property, constant, or variable in order to produce the desired result.
You can use String.Concat(Operators) (MSDN http://msdn.microsoft.com/en-us/library/k9c94ey1.aspx)
You can indeed use String.Join for this:
string sOperators = string.Join("", Operators);
I guess you just forgot to assign the result to a variable.