I have a Multiline textbox who I can paste URL's. The URL's will be paste into a string variable. Now I want, that just URL's from a specific domain can pass into the string variable.
For example:
http://domain-1.tld/gfdgfd.php?=2135346432
http://domain-2.tld/fsefes.php?=2145312542
http://domain-1.tld/random/folders/iwadaex.php?=2112313543
http://domain-2.tld/igewex.php?=2135464432
http://domain-1.tld/folder/inwadawx.php?=2135546432
http://domain-2.tld/ihtfhtf.php?=2143534432
I a have a radiobutton with Domain 1 and Domain 2. If Domain 1 is checked, Only URL's from Domain 1 will be put in the variable, and so on.
I just need to know, how I filter the URL's...
var result = textBox1.Text.Split('\n').Where(x => x.Contains(#"http://" + domain)).ToArray();
Edit after your comment :
var str = String.Join(Environment.NewLine, result);
You can get your lines using Lines property then filter them using Where method:
var filteredUrls = textBoxName.Lines.Where(x => x.Contains(someValue)).ToArray();
Then if you want to display them together, you can use String.Join
var output = string.Join(Environment.NewLine, filteredUrls);
Related
I would like to know how to do this with C #:
I have a CSV file with multiple columns as follows:
I would like to concatenate the result of all the lines of the first column to have:
Name = NDECINT, NDEC, NFAC, ORIGIN .....
You said all c#. This is done with Core 5.
var yourData = File.ReadAllLines("yourFile.csv")
.Skip(1)
.Select(x => x.Split(','))
.Select(x => new
{
Name = x[0] //only working with Name column
,Type = int.Parse(x[1]) //Only added for reference for handling more columns
});
string namesJoined = string.Join(',', yourData.Select(x => x.Name));
This is really basic code and does not handle the crazy things that can be inside a csv like a comma in the name for example.
This solution is for SSIS.
Add a variable called concat set equal to ""
Read the file using SSIS.
Add a script component
Pass in Row A and add variable
Set variable to concat += RowA + ","
When you are done, you will have an extra "," on the variable that needs to be removed.
Use an expression.
concat = left(concat, len(concat)-1)
I have a list of object with some properties like "Name" "Surname" etc...
In a windows forms application I put two textbox one for the name and one for the surname, when the user write something into these textbox the code should filter the list based on what the user has write.
//Let assume that the customer object has two props...name and surname and we have an object called Customers with a long list
List<customer> CustomersFiltered = new List<customer>();
CustomersFiltered = Customers.FindAll((x => x.Name.ToLower().StartsWith(txtName.Text.ToLower())).ToList();
CustomersFiltered = CustomersFiltered.FindAll((x => x.Surname.ToLower().StartsWith(txtSurname.Text.ToLower())).ToList();
//make something amazing withe the CustomersFiltered object
This code function very well, but filter only if the user write the initial of the name or surname. What I need is if the user write "g??fy" the filter has to returns "goofy" but also "gaffy" and so on and if the user write "g*y" the filter has to return "goofy", "gaaaaaaaaaaaaaaaafy", "gngiongiwngiowngwfy". How can I achieve that with Linq?
You need to convert your wildcard to regular expression. You can do this with string.Replace method:
var input = "a?bc*d";
var pattern = input
.Replace("?", "[a-z]")
.Replace("*", "[a-z]*");
Now use Regex.IsMatch method inside lambda
var test = new[] {"axbcd", "abcxxxxxd", "axdcd", "axbcxxxxd" }.ToList();
var match = test.FindAll(x => Regex.IsMatch(x, pattern, RegexOptions.IgnoreCase));
// match == ["axbcd", "axbcxxxxd"]
I am developing an MVC 5 Web Application - I have a screen where a user can tick checkboxes on a grid and this will save the data to the Database. What I need to implement now is the removal of the data if the user navigated back to the screen and unchecked one of the items and then continued.
So the code in my controller so far looks as below:
IEnumerable<string> splitSelectedCars = model.SelectedCars
.Split(',')
.Select(sValue => sValue.Trim());
if (cars.Count > 0)
{
IEnumerable<string> savedCarsInDb = cars.Select(c => c.Id).ToList();
//var merged = splitSelectedCars.Union(savedCarsInDb ,)
//puesdo code - for each value in merged call service layer to remove
}
I am not sure if using a union is the best approach here to find all the values that are in the splitSelected cars list from the model that are not in the savedCarsInDb list and if so what the IEqualityComparer should look like?
So an example list the first time would be 1,2,3,4 passed in to the model - split out and then saved to DB. If the User navigates back and deslects id 4 then splitSelected will have 1,2,3 and savedCarsInDb will still have 1,2,3,4 - so I need to find '4' and then call remove
LINQ can help you here, specificall the Except method:
var selected = model.SelectedCars.Split(',').Select(sValue => sValue.Trim());
var saved = cars.Select(c => c.Id).ToList();
var removed = saved.Except(selected);
Depending upon whether you wish casing to be sensitive or not you can pass in the appropriate string comparer:
var removed = saved.Except(selected, StringComparer.OrdinalIgnoreCase);
Lets say I wanted to create an application for a user to select trouble departments for reporting purposes. The user would go in and select multiple trouble departments from a asp:ListBox and when the user hits send the email would read,
We are having trouble in the following departments: DepartmentA, DepartmentB.
What I have been able to is figure out how to properly loop the items out from the loop, however the last item has a , at the end of the last item. For example instead of reading the proper way as noted above it looks like this:
We are having trouble in the following departments: DepartmentA, DepartmentB,.
Here is my code:
string DeptID = string.Empty;
foreach (ListItem li in lstDSXDepartment.Items)
{
if (li.Selected == true)
{
DeptID += li.Value + ",";
}
}
Response.Write("We are having trouble with the following Departments: " + DeptID + ".");
How do I fix the string so that the comma does not show at the end of list of selections?
You can use string.join. It is much easier.
var ids = lstDSXDepartment.Items
.Cast<ListItem>()
.Where(x=> x.Selected)
.Select(x=> x.Value);
string text = string.Join(",", ids);
Other thought:
If you want to use your original method, you should consider using StringBuilder instead of String because String is immutable.
StringBuilder will significantly improve the performance depending on the number of Items.
Just use a trim function to remove the unwanted comma.
DeptID = DeptID.TrimEnd(',');
Use after the loop, before writing.
Note: The TrimEnd function returns a new copy that is modified of the original string so you have to store it back into your original variable. This is because strings are immutable in C#.
I have a list in which I filter, according to the text input in a TextBox in Xaml. The code below filters the List stored in the results variable. The code checks if the textbox input,ie, queryString, matches the Name of any item in the results list EXACTLY. This only brings back the items from the list where the string matches the Name of a the item exactly.
var filteredItems = results.Where(
p => string.Equals(p.Name, queryString, StringComparison.OrdinalIgnoreCase));
How do I change this so that it returns the items in the list whose Name, is similar to the queryString?
To describe what I mean by Similar:
An item in the list has a Name= Smirnoff Vodka. I want it so that if "vodka" or "smirnoff" is entered in the textbox, the the item Smirnoff Vodka will be returned.
As it is with the code above, to get Smirnoff Vodka returned as a result, the exact Name "Smirnoff Vodka" would have to be entered in the textbox.
It really depends on what you mean, by saying "similar"
Options:
1) var filteredItems = results.Where( p => p.Name != null && p.Name.ToUpper().Contains(queryString.ToUpper());
2) There is also also known algorithm as "Levenshtein distance":
http://en.wikipedia.org/wiki/Levenshtein_distance
http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm
The last link contains the source code in c#. By using it you cann determine "how close" the query string to the string in your list.
Try this:
fileList.Where(item => filterList.Contains(item))
Try this:
var query = "Smirnoff Vodka";
var queryList = query.Split(new [] {" "}, StringSplitOptions.RemoveEmptyEntries);
var fileList = new List<string>{"smirnoff soup", "absolut vodka", "beer"};
var result = from file in fileList
from item in queryList
where file.ToLower().Contains(item.ToLower())
select file;