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 8 years ago.
Improve this question
How will be faster get the file extension
string ext = System.IO.Path.GetExtension(FileName);
string ext=FileName.Substring(FileName.LastIndexOf('.'));
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension;
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];
System.Text.RegularExpressions.Regex extend = new
System.Text.RegularExpressions.Regex(#"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;
In case of such operations your first concern should be which one is more idiomatic, maintainable and readable. The first and the third version are examples of these. In the second and the last one you're trying to reinvent the wheel, making the code less readable and more error prone.
In VM frameworks performance is achieved through higher-level optimisations, like controlling the number of allocated objects, references between them etc. Things you talk about here are minor and probably irrelevant in terms of performance.
Related
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 3 years ago.
Improve this question
In C#, how can I sort strings that separated by special character, like ","
For example I have string like this
string sStates="IA,KY,CT,ME,AZ";
How can I re-sort them alphabetically like
"AZ,CT,IA,KY"
Split them in an Array will work.
But is there a more effective way?
Thanks in advance for help.
This can be done within a single line of code using linq:
var result = string.Join(",", source.Split(",").OrderBy(s => s));
Efficiency considerations should not apply here unless you're doing this for huge strings (or a hugh number of strings) under a tight timeline, or having an actual performance problem.
Readable code is far better then fast code.
As a rule - You should design your code for clarity, not for performance. Write code that conveys the algorithm it is implementing in the clearest way possible. Set performance goals and measure your code's performance against them. If your code doesn't measure to your performance goals, Find the bottle necks and treat them. Don't go wasting your time on nano-optimizations when you design the code.
You can achieve it in this simple way
var orderedString = source.Split(",").OrderBy(p => p);
var result = string.Join(",", orderedString);
I'm a fan of using Lists in .Net personally.
using System.Linq;
var sStates = "IA,KY,CT,ME,AZ";
var orderedList = sStates.Split(',').ToList().OrderBy(o => o);
var orderedString = string.Join(",", orderedList.ToArray());
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 5 years ago.
Improve this question
I'm reading an email and performing a few operations.
In email it can't be in defined format as below,
Email formats:
Hi Team,
Please create 7025-45-365-14, 9851-98-524-12
5741-55-452-45
Thanks
MailItem.Body = "Hi Team,\r\n\r\n \r\n\r\nPlease create 7025-45-365-14, 9851-98-524-12\r\n\r\n5741-55-452-45.\r\n\r\n \r\n\r\nThanks\r\n"
My goal is to exact only the string parts into a loop one by one as below,
1st loop > 7025-45-365-14
2nd loop > 9851-98-524-12
3rd loop > 5741-55-452-45
I tried various logic but I can't extract as I need. Can someone help me?
To extract digits in that pattern (4-2-3-2)
foreach (var m in new Regex(#"\b(\d{4}-\d{2}-\d{3}-\d{2})\b").Matches(mail))
Console.WriteLine(m.Value);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Currently, I have something like the following:
public char functName(int n)
{
some functionality....
if(condition1)
return convertFuncToChar(variable) + functName(modifiedNumber);
else
return convertFuncToChar(variable);
}
however, I realize that doesn't give me a string (and the syntax shows that there's an error).
I know that for c++, I would most likely use char* to initialize the function, but this is C#.
I don't think it works if I initialize with String either.
Assuming I understand the question correctly, you can take advantage of two things:
1) all basic types implement .ToString(), which creates a string for you
2) string implements operator+()
The following is an example of a recursive function that will recursively concatenate characters to create a string. The output is the number in reverse as a string.
static string funcName(int n)
{
if (n<10)
return (n%10).ToString();
return (n%10).ToString() + funcName(n/10);
}
Of course, it would be more efficient to write this non-recursively.
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 8 years ago.
Improve this question
C#:
var articles = Article
.OrderBy(x=> x.Name)
.Where(x=> x.Name.Contains(pattern))
.Select(x=>new {x.Name + " (article)"})
.ToList();
It's good to write this way in C# and it's called "methods concatenation". In fact, I don't remember exactly how they are called, I read it in Jon Skeet's book. The idea is that each method is on a new line, and it's normal in C#.
What about Ruby? Is it normal to write:
articles = Article
.order(:name)
.where("name like ?","%#{pattern}%")
.map(&:name)
.map {|c| c << " (article)"}
Method chaining is a staple in many languages, Ruby included. Its use is largely a matter of personal taste – some, like tokland, don't like the large expressions that can result, whereas I will gladly expand an expression to eliminate temporary variables.
Subjective question, so her's a subjective answer (as Ruby programmer):
I prefer not to create "holes", in my code I wouldn't insert that level of indentation.
You can also insert the dots at the end of the line. Which is more readable? hard to say, I prefer the latter (although I don't mind at the beginning of the line, it's not a big deal). So I'd probably write (note that those two maps could be joined):
article_names = Article.
order(:name).
where("name LIKE ?", "%#{pattern}%").
map(&:name).
map { |name| name + " (article)"}
In my experience, long chains make code harder to follow. When the chain grows too much (5, 6 elements?) I tend to break it creating intermediate variables with meaningful names, this helps me to further describe the expression:
filtered_articles = Article.order(:name).where("name LIKE ?", "%#{pattern}%")
names = filtered_articles.map { |article| "#{article.name} (article)" }
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 7 years ago.
Improve this question
I have a FASTA file containing several protein sequences. The format is like
----------------------
>protein1
MYRALRLLARSRPLVRAPAAALASAPGLGGAAVPSFWPPNAAR
MASQNSFRIEYDTFGELKVPNDKYYGAQTVRSTMNFKIGGVTE
RMPTPVIKAFGILKRAAAEVNQDYGLDPKIANAIMKAADEVAE
GKLNDHFPLVVWQTGSGTQTNMNVNEVISNRAIEMLGGELGSK
IPVHPNDHVNKSQ
>protein2
MRSRPAGPALLLLLLFLGAAESVRRAQPPRRYTPDWPSLDSRP
LPAWFDEAKFGVFIHWGVFSVPAWGSEWFWWHWQGEGRPYQRF
MRDNYPPGFSYADFGPQFTARFFHPEEWADLFQAAGAKYVVLT
TKHHEGFTNW*
>protein3
MKTLLLLAVIMIFGLLQAHGNLVNFHRMIKLTTGKEAALSYGF
CHCGVGGRGSPKDATDRCCVTHDCCYKRLEKRGCGTKFLSYKF
SNSGSRITCAKQDSCRSQLCECDKAAATCFARNKTTY`
-----------------------------------
Is there a good way to read in this file and store the sequences separately?
Thanks
To do this one way is to:
Create a vector where each location
holds a name and the sequence
Go through the file line by line
If the line starts with > then add
an element to the end of the vector
and save the line.substring(1) to
the element as the protein name.
Initialize the sequence in the
element to equal "".
If the line.length == 0 then it is
blank and do nothing
Else the line doesn't start with >
then it is part of the sequence so
go current vector element.sequence
+= line. Thus way each line between >protein2 and >protein3 is
concatenated and saved to the
sequence of protein2
I think maybe a little more detail about the exact file structure could be helpful. Just looking at what you have (and a quick peek at the samples on wikipedia) suggest that the name of the protein is prepended with a >, followed by at least one line break, so that would be a good place to start.
You could split the file on newline, and look for a > character to determine the name.
From there it is a little less clear because I'm not sure if the sequence data is all in one line (no linebreaks) or if it could have linebreaks. If there are none, then you should be able to just store that sequence information, and move on to the next protein name. Something like this:
var reader = new StreamReader("C:\myfile.fasta");
while(true)
{
var line = reader.ReadLine();
if(string.IsNullOrEmpty(line))
break;
if(line.StartsWith(">"))
StoreProteinName(line);
else
StoreSequence(line);
}
If it were me, I would probably use TDD and some sample data to build out a simple parser, and then keep plugging in samples until I felt I had covered all of major variances in the format.
Can you use a language other than C#? There are excellent libraries for dealing with FASTA files and other biological sequence in Perl, Python, Ruby, Java, and R (off the top of my head). They're usually branded Bio* (i.e. BioPerl, BioJava, etc)
If you're interested in C or C++, check out the answers to this question over at Biostar:
http://biostar.stackexchange.com/questions/1516/c-c-libraries-for-bioinformatics
Do yourself a favor, and don't reinvent the wheel if you don't have to.