The Split method in C# and VB.NET - c#

Thanks for someone who might be able to help me further, as I can't find an answer in the www right now. I actually program VB.net, but since this language is now dissolved and not further developed, I want to switch to C#. I am getting on well, because a lot of things are similar. There is only one method where I am stuck.
In vb.net I always liked to use the method:
mystring.split(seperator)(part)
to get a certain string from a CSV line and work with it. How does this work in C#? I know that I can load everything into an array, but I don't want to do that in this case. A simple split and the number of the element is enough. Can someone help me?

Seems like the same split method is what you need? It returns an array which you could store or just access directly. The difference being in C# you use [index] instead. So something like this:
string myFile = File.ReadAllText("myfile.txt");
string firstLine = myFile.Split("\r\n")[0];

Related

Get certain row by searching for a string

I am very new to C# and am trying to feel it out. Slow going so far! What I am trying to achieve should be relatively simple; I want to read a row from a CSV file with a search. I.e. if I search for username "Toby" it would fetch the entire row, preferably as an array.
Here is my users.csv file:
Id,Name,Password
1,flugs,password
2,toby,foo
I could post the code that I've tried, but I haven't even come close in previous attempts. It's a bit easier to do such a thing in Python, it may be easy in C# too but I'm far too new to know!
Does anyone have any ideas as to how I should approach/code this? Many thanks.
Easy to do in c# too:
var lineAsArray = File.ReadLines("path").First(s => s.Contains(",toby,")).Split(',');
If you want case insens, use e.g. Contains(",toby,", StringComparison.OrdinalIgnoreCase)
If your user is going to type in "Toby" you can either concatenate a comma on the start/end of it to follow this simplistic searching (which will find Toby anywhere on the line) or you can split the lone first and look to see if the second element is Toby
var lineAsArray = File.ReadLines("path").Split(',').First(a => a[1].Equals("toby"));
To make this one case insensitive, put a suitable StringComparison argument into the Equals using the same approach as above
Sky's the limit with how involved you want to get with it; using a library that parses CSV to objects that represent your lines with named, typed parameters is probably where I'd stop.. take a look at CSVHelper from josh close or ServiceStack Text, though there are no shortage of csv parser libs- it's been done to death!

convert VB code to C#

I'm a beginner in programming and i would like to know how can i translate the following code into C#
Dim arrayAlumnos(ds.Tables(0).Rows.Count - 1) As Registro
To preserve the array idea I'd probably write it like:
var arrayAlumnos = new Registro[ds.Tables[0].Rows.Count];
But you could say this too:
Registro[] arrayAlumnos = new Registro[ds.Tables[0].Rows.Count];
But ChaosPandion is right... a List is what you'd want to use most likely.
You should be using a list so I think this is the proper translation.
List<Registro> students = new List<Registro>();
you'll probably have a lot of one off questions like this going through your travels of conversion, might want to take a look at:
http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx
The basic idea behind the creation in C# is...
~ObjectType~ ~varName~ = new ~type~(~implement a type constructor here~);
anything in between ~~s you'll need to plug in the appropriate information. For your case it would likely be:
Registro[] arrayAlumnos = new Registro[ds.Tables[0].Rows.Count - 1];
Kind of hard since it's a different spoken language but based on your variable name i'm guessing it's an array, though as others pointed out it could easily be created as a list.
FYI, if you want to convert a lot of code from VB to C#, you can use the ILSpy disassebler. You can do this even if you don't have the original VB code. Do this:
Compile the VB code into a *.exe or a *.dll.
Open the *.exe or *.dll file in ILSpy.
In the language dropdown, select VB. (It's values are C#, VB, and MSIL).

c# - beginner wants to add code on the run from a string containing a math function

I learned basic algorithms on visual C# in highschool, and I made a simple code that numerically integrates a math function within given limits.
I want to be able to change the function the code integrates without actually editing the code, so I googled it for a while and found a lot of articles about how to do it. I tired to understand it but the problem is I can't understand any of what's written there because it's too much above my level.
I need a code that can add code on the run from a string containing a math function, that can accept a variable, log, ln, powers, sin, cos, tan and maybe pi and e, that is ready in a friendly "copy-paste" format, followed by instructions on where to paste it, and how to connect it to my code. To clarify:
I want to take something like this:
string s = "Sqrt(ln(1 + x ^ 2))";
and make it like this:
double x = 0;
double y = Math.Sqrt(Math.Log(1 + Math.Pow(x,2)));
I know it's a pretty annoying request and if it's not the right place to ask such a thing I apologize in advance.
This is actually fairly difficult to do in a language like C#, as it's statically compiled.
A good alternative would be to use an expression parsing library, such as NCalc. This library would allow you to create the expression (your string), parse it, and extract the result.

Use more than one font to drawstring

I want to use drawString to print a sentance but some parts of it has to be bold. What is the best way to do this? Yes, I have considered using two drawString. But is there a intelligent way of using two drawStrings if we have to.
We cannot make any assumtion about the length of the sentance. However it is prepared in a format.
eg:
Say hello to name. Good afternoon
name.
Thanks
This is my old question on MSDN & it was answered. It works good. I hope it is the thing you want to find.
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/8a050039-d74a-4ab5-9237-98615e10e303
Yes, you will have to call DrawString twice - you can write a small wrapper class (or extension method) that will do that for you.

What is the way to apply extension methods?

When attempting to solve the problem
How many seven-element subsets (not repeatable) are there in a set of nine elements ?
I tried
IEnumerable<string> NineSet =new string[] {"a","b","c","d","e","f","g","h","i"};
var SevenSet =
from first in NineSet
from second in NineSet
where first.CompareTo(second)< 0 && first.Count() + second.Count()==7
select new { first, second };
What is the problem that prevents me from attempting to use first.Count() and second.Count()? I did not check whether it is the best solution for the problem.
As already stated, what you have written down will lead you to nowhere. This is a question of combinatorics. AFAIK there is nothing pre-made in the .NET framework to solve for you combinatorics problems, hence you will have to implement the correct algorithm. If you get stuck, there are solutions out there, e.g. http://www.codeproject.com/KB/recipes/Combinatorics.aspx, where you can look at the source to see what you need to do.
first and second are strings, so you'll count their characters (this compiles, but intellisence hides it).
You're looking for something like NineSet.Count(first.Equals)
Well...
You haven't shown the error message, so it's hard to know what's wrong
Every element is of length exactly one, so I'm not sure what you're expecting to happen
As you know that first and second are strings, why aren't you using first.Length and second.Length?
As a side issue, I don't think this approach is going to solve the problem for you, I'm afraid...

Categories

Resources