|
Attention Member 12106926: Please note the code shown here was written a few years ago, and has not been tested, recently. I would strongly advise you to use this code (if you find it useful) only as the basis for your own code, and, as always, to test thoroughly. Well, Sir, I am reluctant to step up to the plate when heavy-hitters like Simmons and Deeming are in the ... I'll be lucky if I don't get brained by a curvew-ball, let alone strike-out with both of them, but, I like to try and create generic solutions to tasks like cleaning-up (getting rid of unwanted whatevers at the start and end of a collection), and parsing. Here's a generic parser based on Extension Methods that will parse the OP's sample, producing an IEnumerable of List<char>:
public static class IEnumerableExtensions
{
public static IEnumerable<List<T1>> GroupByContiguous<T1>(this IEnumerable<T1> src, T1 delimiter)
{
var src1 = src.TrimStart<T1>(delimiter)
.TrimEnd<T1>(delimiter);
List<T1> srcList = src1.ToList();
T1 current;
T1 previous = srcList.First();
List<T1> element = new List<T1>();
element.Add(previous);
for (int i = 1; i < srcList.Count; i++)
{
current = srcList[i];
if (current.Equals(delimiter))
{
previous = delimiter;
continue;
}
if (! current.Equals(previous))
{
yield return element;
element = new List<T1>();
previous = current;
}
element.Add(current);
}
if (element.Count > 0) yield return element;
}
public static IEnumerable<T> TrimStart<T>(this IEnumerable<T> totrim, T trimthis)
{
return totrim.SkipWhile(itm => itm.Equals(trimthis));
}
public static IEnumerable<T> TrimEnd<T>(this IEnumerable<T> totrim, T trimthis)
{
return totrim.Reverse().SkipWhile(itm => itm.Equals(trimthis)).Reverse();
}
} Test with OP's data (with some extra delimiters thrown in for variety):
private string text = "-DDDD-AAAA-DAAD-ADDDAA---";
var parsedChars = text.GroupByContiguous<char>(delimiter: '-').ToList();
foreach (var grp in parsedChars)
{
Console.WriteLine(new string(grp.ToArray()));
}
DDDD
AAAA
D
AA
D
A
DDD
AA I've tested on basic ValueType and had no errors, but, an obvious issue when you start comparing complex objects (like instances of Classes) is: will using .Equals for the equality test suffice, or will you have to provide a custom implementation of IComparer.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
I think he wanted to know where the delimiters were as well.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Interesting comment, John,
There are a lot of ways you could skin this cat
The result of the code I showed, because it created new groups with every break in "continuity," would not reveal exactly where the specific delimiters were.
The OP does say: "separators are not mandatory."
imho, that raises the question of why use specific delimiters; perhaps they have other purposes, perhaps the OP's intent is not clear ?
But, it's trivial to get a list of indexes of some specific char; I use an Extension method to do this, but the essence is simple:
0. create a List of Int to hold the result
1. convert the string to a List of char;
2. use 'FindIndex and 'FindLastIndex to get the first and last position of the delimiter.
3. for-loop over the range from first index to last index
4. if the char at the index position is a delimiter, add the index value to the list.
While you could Linq to do this:
List<int> ndxs = str.Select((ch, i) => (ch == delimiter) ? i : -1).Where(i => i > -1).ToList(); It's much faster to do it with a 'for loop.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
$.ajax({
type: "POST",
url: '/Account/Account_Details.aspx/Datafordownload',
data: "{'Transactionid':'" + arg + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = JSON.parse(response.d);
var a = result["Data"][0]["CompanyName"];
$('#lbl_companyname').html(a);
$('#lbl_address').text(result["Data"][0]["Address"]);
$('#lbl_city').text(result["Data"][0]["CityName"]);
$('#lbl_state').text(result["Data"][0]["StateName"]);
$('#lbl_zipcode').text(result["Data"][0]["ZipCode"]);
$('#lbl_email').text(result["Data"][0]["EmailId"]);
$('#lbl_transc_date').text(result["Data"][0]["StartDate"]);
$('#lbl_transtype').text(result["Data"][0]["TransactionDescription"]);
$('#lbl_desc').text(result["Data"][0]["Description"]);
$('#transactiondate').text(result["Data"][0]["StartDate"]);
$('#lbl_accountbalance').text(result["Data"][0]["NetBalance"]);
$('#lbl_credit').text(result["Data"][0]["Credit"]);
},
|
|
|
|
|
Does this have something to do with C#?
|
|
|
|
|
yes after binding data in label i will use html 2canvas for downloading dynamic data which is binded in label
|
|
|
|
|
|
I think what Richard was trying to get are you sure that this is the best forum for this?
ASP.NET Discussion Boards[^] will be more suited to this type of question
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Hi!
I need to create a capturing console in my c# website where a PDF can be opened in the website and then you would click in the textbox eg. surname and then highlight the surname in the PDF document and then it enters the info into the textbox.
Any ideas on how I can accomplish this?
Thanks!
|
|
|
|
|
|
how can we connect a virtual RFID reader created using Rifidi to an application developed using C#.NET?
|
|
|
|
|
This[^] would certainly be a better choice to ask question about this specific product.
You always obtain more by being rather polite and armed than polite only.
|
|
|
|
|
How to work on toggle button when two text fields in which we need one field require and save data and another second filed of text control toggle button.
Pleaze help me.
|
|
|
|
|
I would, if I understood what you are trying to do and what problem you are having doing it.
But...we can't see your screen, access your HDD, or read your mind and all we get to work with is exactly what you typed - we get no context beyond that.
So tell us what system you are trying to work with (web, WinForms, WPF, Xamarin), explain what you ant to do in a lot more detail, tell us what you have tried, and where you are stuck. Tell us what it does that you didn't expect, or didn't do that you did. Tell us what help you need!
And we'll do our best to assist.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I'm extremely lost on how to do this...
If anyone could help, it would be greatly appreciated.
I need to write an application in c# that continuously prompts a user for test scores until the user enters a sentinel value. A valid score ranges from 0 through 100. When the user enters a valid score, add it to a total; when the user enters an invalid score, display an error message and before the program ends, display the number of scores entered and their average.
The code needs to contain a while loop.
Thanks!
|
|
|
|
|
|
suppose these are two classes for which table has been created in db.
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
suppose if i add two more fields in two above class but i need to migrate the changes only for post and ignore blogs changes. so what i need to do ?
any way exist?
tbhattacharjee
|
|
|
|
|
I would HIGHLY suggest you back out the changes you don't want to end up in the migration, recompile and do your migration, then put the non-migration changes back in.
Otherwise you're writing the migration code by hand and that's a huge pain in the ass to write and manage. Hopefully you don't screw anything up and end up borking your next migration.
|
|
|
|
|
Sorry i do not understand what you try to say. what does this mean "you back out the changes you don't want to end up in the migration" ?
are you trying to say comment those classes which i want to exclude during migration ?
please tell me step-by-step how to achieve what i am looking for. thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: are you trying to say comment those classes which i want to exclude during migration ?
Yes. Not just new classes you add but any changes to existing classes.
Tridip Bhattacharjee wrote: please tell me step-by-step how to achieve what i am looking for. thanks
The problem with a "step-by-step" description is that every guide has to be custom written for your situation and don't tell you anything about why you're doing the things you are.
The best thing here is to understand migrations before you try this on your own code. Once you understand how migrations work you don't need a custom written guide.
Entity Framework Code First Migrations[^]
Google - Entity Framework Migrations[^]
|
|
|
|
|
Hi,
I Have a C# windows application and it is connected to MS SQL database using direct connection, the database is in server and the application in a client machine (other PC).
My question is when I save a transaction from client PC with datetime to a table in the sql databse on the server (DateTime.Now) get the client datetime, I need to insert the Server datetime not the client, please let me know is it possible to do this.
Insert into XYZ(ID,Name,InsertDate) Values (1,'Test',DateTime.Now)
I need DateTime.Now to be server datetime, and Don't need to use getDate() sql.
Thank You
|
|
|
|
|
its not clear how you're putting that insert together - I hope not concatenation, but you're building a parameterised query .. anyway
MySQL has a date/time function NOW() MySQL Date and Time Functions[^] - my thoughts are that that is a server datetime
whoops, you wanted MSSQL not MySQL - sorry .. how about SYSDATETIME() Date and Time Data Types and Functions (Transact-SQL)[^] - description looks correct 'instance on which MSSQL is running'
|
|
|
|
|
Yes, it is parameterized and not a concatenation one, but I don't need to use sqldate I need to read the date from server and insert it again
thanks
|
|
|
|
|
If you want the actual server Date and Time value, then you have little choice but to use GETDATE() - that is the only way to get the server setting. You could "prefetch" it, but if the timestamp is important that's a bad idea and it could be out by seconds or more by the time you use it.
Or you could use GETUTCDATE() which returns a universal value rather than one based on the server locale settings.
If you don't want to call it directly, the other option is to add one of the two function calls as the default value for that column when you define the table - that way the system will set it for you if you don't specify it.
And please, use parameterised queries rather than passing any values as part of the SQL command string directly - you leave your application wide open to SQL Injection attacks if you don't.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
A simpler method is to not pass InsertDate across at all. Instead, set it to use a default value of GETUTCDATE(), so whenever you insert a row, the UTC date will be applied. That way, you don't have to worry about calling the server functions.
This space for rent
|
|
|
|