|
Hi,
I want to split this string:
"text 1, text 2, text 3, text 4"
I want the result to be like this:
"text 1"
"text 2"
"text 3"
I tried this code:
string.split(',').ToList();
The problem is that there is an extra space in list items:
"text 1"
" text 2"
" text 3"
How can I remove those extra spaces?
modified 11-Aug-21 5:21am.
|
|
|
|
|
You need to call Trim() on each item before saving it. something like:
List<string> words = source.Split(',').Select(s => s.Trim()).ToList();
|
|
|
|
|
You could always do
string.Split(", ", StringSplitOptions.None);
|
|
|
|
|
Are you sure?
Quote: I want to split this string:
"text 1, text 2, text 3, text 4"
I want the result to be like this:
"text 1"
"text 2"
"text 3"
Ignore me - I assumed it would treat the string as a char array and split on each letter ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have this method that returns a List<jobstartdate>. Each start date can have 1-n revisions. So after I get the start dates I then loop through them all and get the revisions for each one.
My question is this... is it possible to somehow do this all in one query without having to loop and get the child records? Can I somehow select the joined records into entities in the same query as the start dates?
<pre>public List<JobStartDateEntity> GetJobStartDates(int jobId)
{
using (var dc = GetDataContext())
{
var results = (from jsd in dc.JobStartDates
where jsd.JobId == jobId
select new JobStartDateEntity
{
Id = jsd.Id,
StartDateId = jsd.StartDateId ?? 0,
JobId = jobId,
ProjectStartDateId = jsd.ProjectStartDateId,
Caption = jsd.Caption,
Description = jsd.Description,
Sequence = jsd.Sequence,
DaysOffset = jsd.DaysOffset,
StartDate = jsd.StartDate
}).ToList();
foreach (var result in results)
{
var startDateRevisions = (from sdr in dc.JobStartDateRevisions
where sdr.Id == result.Id
select new JobStartDateRevisionEntity
{
Id = sdr.Id,
JobId = sdr.JobId,
StartDate = sdr.StartDate,
Revision = sdr.Revision
}).OrderByDescending(x => x.Revision).ToList();
result.StartDateRevisions = startDateRevisions;
}
return results;
}
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Assuming you have a navigation property, you may be able to do something like this:
var jobStartDates = dc.JobStartDates.AsNoTracking().Include(jsd => jsd.Revisions).Where(jsd => jsd.JobId == jobId).ToList();
var results = jobStartDates.Select(jsd => new JobStartDateEntity
{
Id = jsd.Id,
...
StartDateRevisions = jsd.Revisions.Select(sdr => new JobStartDateRevisionEntry
{
Id = sdr.Id,
...
}).OrderByDescending(sdr => sdr.Revision).ToList(),
});
Alternatively, you can do it with two queries, rather than N+1 queries:
var results = (from jsd in dc.JobStartDates
where jsd.JobId == jobId
select new JobStartDateEntity
{
...
}).ToList();
var jobStartDateIds = results.Select(jsd => jsd.Id).ToList();
var revisions = dc.JobStartDateRevisions
.Where(sdr => jobStartDateIds.Contains(sdr.Id))
.Select(sdr => new JobStartDateRevisionEntity { ... })
.ToList();
forech (var result in results)
{
result.StartDateRevisions = revisions
.Where(sdr => sdr.Id == result.Id)
.OrderByDescending(sdr => sdr.Revision)
.ToList();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Without a navigation property, wouldn't a join be easier and faster?
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Hi,
I want to check Persian date in these formats:
yyyy/MM/dd ---- example: 1400/05/17
yyyy/M/d ---- example: 1400/5/17
yy/MM/dd ---- example: 00/05/17
yy/M/d ---- example: 00/5/17
The input date should be in one of those formats. I want to create True/False expression based on the input string.
Please guide me
|
|
|
|
|
|
I coded this class:
public string dateText { get; set; }
DateTime date;
string[] formats = {"yyyy/MM/dd", "yyyy/M/d", "yy/MM/dd", "yy/M/d" };
DateTime result;
public bool CheckFormat()
{
if (!DateTime.TryParseExact(dateText, formats, CultureInfo.InvariantCulture,DateTimeStyles.None, out result))
{
return false;
}
else
{
return true;
}
}
|
|
|
|
|
|
Nope, I don't get any.
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
If you want help, you need to explain what you have tried, where you are stuck, and what help you need. Otherwise, we can't work out what the heck you are talking about and really can't help you.
Typing as little as possible mean you don't get useful answers!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
I have written a simple class for extracting numbers from a text:
using System;
namespace NumExtract
{
public class Extraction
{
public string text { get; set; }
string b;
public int value;
public int NumberExtract()
{
for (int i = 0; i < text.Length; i++)
{
if (char.IsDigit(text[i]))
{
b += text[i];
}
}
value = int.Parse(b);
return value;
}
}
}
When I use this dll file as a reference in my project, this error appears:
Quote: System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'
This error appears in Program.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.UserSkins.BonusSkins.Register();
Application.Run(new Form1());
}
How can I fix it?
|
|
|
|
|
|
I have made a dll class library and imported it to my references in visual studio. I can use its namespace but it causes runtime error during the compiling.
|
|
|
|
|
Finally, I found the problem. I tried to use C# class library (NetFrameWork) instead of C# class library core.
|
|
|
|
|
Yes, targeting can be quite complicated these days.
If the library targets .NET Framework, then it can be referenced by applications targeting any version of .NET Framework greater than or equal to the target version. But it cannot be referenced by applications targeting .NET Core or .NET 5/6.
If the library targets .NET Standard 2.0, then it can be referenced by applications targeting .NET Framework 4.6.1 or later, .NET Core 2.0 or later, or .NET 5/6. But you won't be able to use the new features introduced in .NET Core 3.0 or .NET 5/6.
If the library targets .NET Standard 2.1, then it can be referenced by applications targeting .NET Core 3.0 or later, or .NET 5/6. It cannot be referenced by applications targeting any version of .NET Framework. You will be able to use the new features introduced in .NET Core 3.0, but not those introduced in .NET Core 3.1 or .NET 5/6.
If the library targets .NET Core, then it can be referenced by that version of .NET Core, later versions of .NET Core, or .NET 5/6. It cannot be referenced by applications targeting any version of .NET Framework.
If it targets .NET 5, then it can only be referenced by applications targeting .NET 5/6.
This is Microsoft's idea of simplifying things!
.NET Standard | Microsoft Docs[^]
Oh, and there's also the option of multi-targeting, in case the above options weren't complicated enough:
Multi-targeting for NuGet Packages in your project file | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Why such a complex piece of code to solve a simple problem?
public int NumberExtract()
{
if (!Int32.TryParse(text, out value)
{
throw new FormatException("text is not a valid number");
}
return value;
}
|
|
|
|
|
Maybe the numerals exist inside text that also contains alpha characters.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
If you throw an exception when input is invalid you can use Parse instead
|
|
|
|
|
If you think the problem is DevExpress related, try their (excellent) support forums.
public bool TryGetDigits(string str, out int iresult)
{
StringBuilder sb = new StringBuilder
(
new string(str.Where(ch => Char.IsDigit(ch)).ToArray())
);
if (Int32.TryParse
(
sb.ToString(),
out iresult)
)
{
return true;
}
return false;
} usage:
int iresult = 0;
bool gotSomeDigits = TryGetDigits("2ergtgwegwg1", out iresult);
if (gotSomeDigits )
{
}
else
{
}
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
I'm not sure why you've got the StringBuilder in there? You've already created the string you need and passed it into the constructor.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: I'm not sure why you've got the StringBuilder in there? Because little embroidered habanera peppers on old blue jeans is ... intriguing ?
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Hi.
I use C# in Visual Studio and SQL Server. In my form I have a label that receives the name of a bank and a Textbox that must be filed with the bank balance. This couple repeats for the number of bank accounts of the client. After I retrieve the balances, I use the following snipet:
if (TabSaldo.Rows.Count > 0)
{
for (i = 0; i < Nbancos; i++)
{
foreach (Control c1 in Gb_Bancos.Controls)
{
if (c1.Name.Contains("Lb_Banco" + (i + 1).ToString()))
c1.Text = LinhaSaldo[i].ItemArray[1].ToString();
if (c1.Name.Contains("Tb_Banco" + (i + 1).ToString()))
{
c1.Text = LinhaSaldo[i].ItemArray[2].ToString();
TotalBancos += float.Parse(LinhaSaldo[i].ItemArray[2].ToString()) / 100;
}
}
}
}
It's working fine, but when I make the conversion from string to float (line starting with (TotalBancos +=) the conversion ignores the decimal point. Example: if the balance is 113,54 it sums 11354. As you see, I had to include a division by 100, for it to work. Please does anyone know what is wrong here?
Thanks.
|
|
|
|