string similarity and pattern matching [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Say I have strings like these
Sam is Boy
Joseph is Boy
Jasmine is Girl
Annie is Girl
Chris is Boy
I have a quick and murky way of preparing a C# Dictionary like this..!
input.ForEach(i =>
{
string[] values = i.Split();
input_dictionary.Add(values[0], values[2]);
});
Do we have any other better/optimised way of achieving this, since the input data follows a fixed format like "Name is Gender"?

Here's a regex pattern you could use:
(.+) is (Boy|Girl)

The following is probably faster, but you should test it.
input_dictionary = (from i in input
let n = i.IndexOf(' is ')
select new { Name = i.Substring(0, n), Sex = i.Substring(n + 4) }
).ToDictionary(i => i.Name, i => i.Sex);
You can also the Regex class, which might be faster or slower than the above. It's difficult to say without testing.

Related

Remove last character in a list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am building a list from a dataset as 12345_9876,125675_0987,
I try to remove the last , from the list like we do for strings..
value.TrimEnd(',');
Can we do similar thing for a list?
I hope this will help you?
var lastElement = yourList.ElementAt<string>(yourList.Count - 1).TrimEnd(',');
yourList.RemoveAt(yourList.Count - 1);
yourList.Insert(yourList.Count,lastElement);
Think you just want this?:
List<string mylistfromdataset = // I dunno how you populated it
var newlist = mylistfromdataset.Select(x=> x.TrimEnd(',')).ToList();
EDIT after the poster cleared the question, you could use this 2 line code to make a new list where the last string the , is trimmed.
var list = listofstring.Take(listofstring.Count - 1).ToList();
list.Add(listofstring.Last().TrimEnd(','));
(Make a list excluding the last item. then trim the last item with , at the end and at it to the new list.)
Just use String.Join:
String delimitedList = String.Join(",", yourList);
int i=value.LastIndexOf(",");
if(i!=-1)
value=value.Remove(i,1);

Looping through an array objects within another array of objects [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array of object called objStud within it has another array
how do I loop and display them?
Here is an image that you can see how objStud is like
Just use nested foreach statements
foreach (Student stud in objStud)
{
foreach (Fee in stud.Fees)
{
// Do something with stud and/or fee
}
}
You can use SelectMany to flatten first:
foreach(var fee in objStud.SelectMany(x => x.Fees))
{
}
You can use "foreach" in looping and displaying the values. This is a very easy and good example on how to access the data that you want to display.
This will also teach you the concept behind array. Have fun!

Regex for two years separated by a forward slash [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am new to learning Regex and I am struggling with this basic issue. I want to make sure a string is in a format like: 2000/2001 or 2010/2011.
I tried something like: ^[2000-2900]./.[2000-2900]$ but I know this is wrong!
This would be the very basic:
^\d{4}\/\d{4}$
From the beginning of the string, check if it has 4 digits followed by a "/" (escaped with "\") and another 4 digits to the end of the string.
If you searching for where the entire string must match then:
^\d{4}/\d{4}$
If you are searching for a sub string of a larger string then:
\d{4}/\d{4}
And if you using in C# then remember to wrap it up in a verbatim string like so:
#"^\d{4}/\d{4}$"
#"\d{4}/\d{4}"
I noticed that others are escaping the forward slash but I don't think is necessary but doesn't do any harm if you do.

C# Creating a list for events that did not occur today [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I`m making an application for an animal shelter, and each time a dog is added to the animal list it has to have the date it last went for a walk. Part of the assignment is to show a list of dogs that are not walked out TODAY. Any ideas?
OK, assuming that you have a "LastWalked" column in your data structure you want something like:
var dogsNotWalked = allDogs.Where(d => d.LastWalked < DateTime.Today);
It would help if you'd try something before asking for help. Not to mention showing your work. It's not exactly difficult, a one-liner, in fact. Try something like this:
public IEnumerable<Dog> Dogs { get ; set ; }
public IEnumerable<Dog> FindDogsNotWalkedRecently( DateTime referenceDate )
{
return Dogs.Where( dog => dog.LastWalkedAt < referenceDate ) ;
}

Get words start with # like facebook mentions by regex and javascript [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
first of all i know there is many topics talks about this case but not like i want
i want to get the words like Facebook do in mentions names in comments
here is my conditions
1- i want all words start with # char
2- i want all lonely # char
3- i don't want any words that contain '#' like any#hotmail.com
EX: "# this d#y #hmed #nd May# went to play # g#arden"
the result i want is
{"#" , "#hmed" ,"#nd" , "#"}
please notice the second condition i want
thanks
The regex should be /(?:^|\s)(#[^#\s]*)(?=\s|$)/g
[ test: http://ideone.com/1LK80 ]

Categories

Resources