C# Sort / Order a textBox with given Values - c#

I want to sort / order a multiline textbox with given values, but it seems I do something wrong. It does not sort / order. \r\n has also no effekt. The values were created by hand for testing.
Could someone please help me?
string[] test = { "2021-12-08 2020-04-12 2021-06-15 2022-11-28 2019-01-12" };
//string[] test = { "2021-12-08" + "2020-04-12" + "2021-06-15" + "2022-11-28" + "2019-01-12" }; Also tested and with whitespace.
IEnumerable<string> query = test.OrderBy(i => i.ToString());
foreach (string r in query)
{
textBox2.Text = r.ToString()+"\r\n";
}

Related

remove the last second character from string

I had a query string end with :
where id in (.....,11,)
and I want to remove the last "," to work correct I try this :
string test = id_case[i];
id_case[i] = test.Substring(Math.Max(0,test.Length -2));
id_case[i] += test.Substring(Math.Max(test.Length-1,test.Length)) + ")";
but didn't work the whole " where .... " is disappear
any help ?
This will remove the last comma in your query string:
var q = "where id in (.....,11,)";
q = q.Remove(q.LastIndexOf(','),1);
It would probably be cleaner to remove the comma at the source, but one method is to do:
string[] parts = test.Split(',');
test = string.Join(",",parts.Take(parts.Length - 1))
+ parts[parts.Length-1);

Crystal report textobject and fields

i am running the following code
foreach (ReportObject obj in oSectionObjects)
{
if (obj.Kind == CrystalDecisions.Shared.ReportObjectKind.TextObject)
{
// do stuff
}
}
but i have a problem. i do have multiple text that do contain text AND fields in them.
But crystal return me the field being TextObject which is technically true.
How do i know i ONLY have text in the TextObject and not anything else (aka fields, parameters, formulas) ?
As far as I know the fields in a text box will be recognized by the text pattern. Try to search the text of the text object for {1#xxxxx} where xxxxx is the field name. "{1#" shows the type of the field: 1 is for a database , 2 is for formula, 3 is for parameter. You may try also for {#xxxxx} *(without numeric field identifier)
I searched alot around and found working solution for RAS report but nothing for crystal. Anyhow if someone end up here looking for an answer here's the work around.
Whenever you have to concatenate multiple fields on the report do NOT use TextObject. Instead use a Formula. The formula fields wont bet part of the ReportObjects but instead part of the ReportDocument.DataDefinition.FormulaFields with Kind being CrystalDecisions.Shared.FieldKind.FormulaField and you will want to check the ValueType so it is CrystalDecisions.Shared.FieldValueType.StringField.
then you can manipulate them.
I did need that for translation of report live so here's a parsing method for formulas :
try
{
var sFormula = formula.Text;
string pattern = "\"[\\w ]*\"";
Regex r = new Regex(pattern);
MatchCollection mc = r.Matches(sFormula);
foreach (Match m in mc)
{
var sValue =m.Value;
var sParsedValue = sValue.Substring(1, sValue.Length - 2);
if (sParsedValue.StartsWith("s"))
{
var stest = "\"" + CApplicationData.TranslateStringValue(sParsedValue) + "\"";
sFormula = sFormula.Replace(sValue, stest);
}
}
formula.Text = sFormula;
}
catch{}
this above you will notice i use 's' as a key to know it might be a value to be translated so it's not mandatory. using the above on this formula with Spanish language :
"sPage" + " " + totext(PageNumber) + " " + "sOf" + " " + totext(TotalPageCount)
will modify the formula to :
"Página" + " " + totext(PageNumber) + " " + "de" + " " + totext(TotalPageCount)
giving output of :
Página 1 de 4

Validating header names for an SQL column list

I am currently trying to make a user friendly input for people to input SQL headers for a CSV that is created using a temporary table, however I am having issues with validating and changing the names to SQL friendly column headers.
An example input would be as follows:
Name, Ag-e, Gender, Birth Place, Rac+e
Please keep in mind that the input could be ANY word, these are simply an example.
My ideal final output would for the SQL column headers
name age gender birth_place race
however I am having issues checking for invalid characters (which I haven't actually got around to yet.) but my primary issue I am currently having is checking for spaces between words that SHOULD have a space and other spaces at the start of words.
My current output is coming out as(please note that the invalid characters are for testing later.):
Name Ag-e Gender Birth Place Rac+e
Please note that there are double spaces between every one apart from Birth Place which has a single space as it should.
The code I am currently using to achieve this (or not achieve as you can clearly see) is:
columnNamesList = new List<string>(columnNames.Split(splitChar));
columnNamesList[0] = columnNamesList[0].Trim();
columnNamesList[columnNamesList.Count - 1] = columnNamesList[columnNamesList.Count - 1].TrimEnd();
List<string> removalList = new List<string>();
foreach (string i in columnNamesList)
{
if (string.IsNullOrEmpty(i))
{
removalList.Add(i);
}
}
if (removalList.Count < 0)
{
foreach (string i in removalList)
{
columnNamesList.Remove(i);
}
}
for (int i = 0; i < columnNamesList.Count; i++)
{
string s = string.Empty;
string str = columnNamesList[i];
if (Regex.IsMatch(str, #"\w\s\w+", RegexOptions.IgnoreCase))
{
foreach (char c in str)
{
if (Char.IsLetterOrDigit(c) || c == ' ' || c == ',')
s += c;
s = s.Replace(' ', '_');
columnNamesList[i] = s;
}
}
}
string[] columnArray = columnNamesList.ToArray<string>();
columnNames = String.Join(" ", columnArray);
I thought you said that the input is like the first string, comma separated.
Does this not work? All you have to do is remove the unwanted characters (against a blacklist)
var input = "Name, Ag-e, Gender, Birth Place, Rac+e";
var splitInput = input.Split(',')
.Select(i =>
i.Trim()
.ToLower()
.Replace(' ','_'));
var output = string.Join(" ", splitInput.ToArray());

String Format with undefined number of characters c#

So I'm working on formatting a string and I need to line it up in a table, but this string has an undetermined number of characters. Is there anyway to have the string be in the same spot for each column? so far I have:
ostring += "Notes\t\t"
+ " : "
+ employees[number].Notes
+ "\t\t"
+ employees[number].FirstNotes
+ "\t\t"
+ employees[number].SecondNotes;
I use a similar fashion on the other rows, but they have a pre-determined number of digits, this however doesn't so I can't use the string modifiers like I would like.
Any ideas on what I need to do?
You can use String.PadRight() to force the string to a specific size, rather than using tabs.
When you are using String.Format item format has following syntax:
{ index[,alignment][ :formatString] }
Thus you can specify alignment which indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer).
Also it's better to use StringBuilder to build strings:
var builder = new StringBuilder();
var employee = employees[number];
builder.AppendFormat("Notes {0,20} {1,10} {2,15}",
employee.Notes, employee.FirstNotes, employee.SecondNotes);
You would first have to loop over every entry to find the largest one so you know hoe wide to make the columns, something like:
var notesWidth = employees.Max(Notes.Length);
var firstNotesWidth = employees.Max(FirstNotes.Length);
// etc...
Then you can pad the columns to the correct width:
var output = new StringBuilder();
foreach(var employee in employees)
{
output.Append(employee.Notes.PadRight(notesWidth+1));
output.Append(employee.FirstNotes.PadRight(firstNotesWidth+1));
// etc...
}
And please don't do a lot of string "adding" ("1" + "2" + "3" + ...) in a loop. Use a StringBuilder instead. It is much more efficient.

C# string.Contains using variable

string[] pullspec = File.ReadAllLines(#"C:\fixedlist.txt");
foreach (string ps in pullspec)
{
string pslower = ps.ToLower();
string[] pslowersplit = pslower.Split('|');
var keywords = File.ReadAllLines(#"C:\crawl\keywords.txt");
if (pslower.Contains("|"))
{
if (pslower.Contains(keywords))
{
File.AppendAllText(#"C:\" + keyword + ".txt", pslowersplit[1] + "|" + pslowersplit[0] + "\n");
}
}
}
This doesn't compile because of pslower.Contains(keywords) but I'm not trying to do 100 foreach loops.
Does anybody have any suggestions?
Using LINQ:
if (keywords.Any(k => pslower.Contains(k)))
You have a collection of keywords, and you want to see if any of them (or all of them?) are contained in a given string. I don't see how you would solve this without using a loop somewhere, either explicit or hidden in some function or linq expression.
Another solution - create a String[]of the keywords and then string[] parts = pslower.Split(yourStringArray, StringSplitOptions.None); - if any of your strings appear then parts.Length > 1. You won't easily get your hands on the keywords this way, tho'.

Categories

Resources