Excel Delimiter Splitting - asp.net

I have an excel file and I am splitting cell content based on special charectors as shown below:
dt =new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("Skills");
dt.Columns.Add("counts");
string name = rdr[0].ToString();
string age = rdr[1].ToString();
char[] s = new char[1] { ';' };
char[] k=new char[1] {'#'};
string[] skills = rdr[2].ToString().Split(s);
string[] counts = rdr[3].ToString().Split(k);
(ie) If i have below table
Name kk
Age 31
Skills .net;sqlserver;oracle
counts 1#2#3#
output will be :
kk 31 .net 1
sql 2
oracle 3
I am successfull in achieveing above output.
Required Output1:
if you have 1## 2## 3## in counts column instead of 1#2#3#,the how do i split below expression?
char[] k=new char[1] {'#'};
Required Output2:
how to split expression (char[] k=new char[1] {'#'};
---> if i have both ## and # (ie) like: 1## 2# 3# in 'counts' column.
here also i want output as :
1
2
3
Hope I am clear.Please help me in sorting this issue.
Thanks,
Kranthi

Do like this:
string[] counts = rdr[3].ToString().Replace("##","#").Split(k);

Related

Format datetime day with st, nd, rd, th

I'm creating a report in SSRS and across the top I have a header with a placeholder for "Last Refreshed" which will show when the report last ran.
My function in the placeholder is simply this:
=Format(Now, "dddd dd MMMM yyyy hh:mm tt")
Which looks like this:
Monday 22 September 2015 09:46 AM
I want to format the day value with the English suffix of st, nd, rd and th appropriately.
I can't find a built in function for this and the guides I've looked at so far seem to describe doing it on the SQL side with stored procedures which I don't want. I'm looking for a report side solution.
I thought I could get away with an ugly nested IIF that did it but it errors out despite not giving me any syntax errors (whitespace is just for readability).
=Format(Now, "dddd " +
IIF(DAY(Now) = "1", "1st",
IIF(DAY(Now) = "21","21st",
IIF(DAY(Now) = "31","31st",
IIF(DAY(Now) = "2","2nd",
IIF(DAY(Now) = "22","22nd",
IIF(DAY(Now) = "3","3rd",
IIF(DAY(Now) = "23","23rd",
DAY(Now) + "th")))))))
+ " MMMM yyyy hh:mm tt")
In any other language I would have nailed this ages ago, but SSRS is new to me and so I'm not sure about how to do even simple string manipulation. Frustrating!
Thanks for any help or pointers you can give me.
Edit: I've read about inserting VB code into the report which would solve my problem, but I must be going nuts because I can't see where to add it. The guides say to go into the Properties > Code section but I can't see that.
Go to layout view. Select Report Properties.Click on the "Code" tab and Enter this code
Public Function ConvertDate(ByVal mydate As DateTime) as string
Dim myday as integer
Dim strsuff As String
Dim mynewdate As String
'Default to th
strsuff = "th"
myday = DatePart("d", mydate)
If myday = 1 Or myday = 21 Or myday = 31 Then strsuff = "st"
If myday = 2 Or myday = 22 Then strsuff = "nd"
If myday = 3 Or myday = 23 Then strsuff = "rd"
mynewdate = CStr(DatePart("d", mydate)) + strsuff + " " + CStr(MonthName(DatePart("m", mydate))) + " " + CStr(DatePart("yyyy", mydate))
return mynewdate
End function
Add the following expression in the required field. I've used a parameter, but you might be referencing a data field?
=code.ConvertDate(Parameters!Date.Value)
Right Click on the Textbox, Go To Textbox Properties then, Click on Number tab, click on custom format option then click on fx button in black.
Write just one line of code will do your work in simpler way:
A form will open, copy the below text and paste there to need to change following text with your database date field.
Fields!FieldName.Value, "Dataset"
Replace FieldName with your Date Field
Replace Dataset with your Dateset Name
="d" + switch(int(Day((Fields!FieldName.Value, "Dataset"))) mod
10=1,"'st'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 =
2,"'nd'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 =
3,"'rd'",true,"'th'") + " MMMM, yyyy"
I found an easy way to do it. Please see example below;
= DAY(Globals!ExecutionTime) &
SWITCH(
DAY(Globals!ExecutionTime)= 1 OR DAY(Globals!ExecutionTime) = 21 OR DAY(Globals!ExecutionTime)=31, "st",
DAY(Globals!ExecutionTime)= 2 OR DAY(Globals!ExecutionTime) = 22 , "nd",
DAY(Globals!ExecutionTime)= 3 OR DAY(Globals!ExecutionTime) = 23 , "rd",
true, "th"
)

Generate Conditional Code From String

I want to generate conditional code of the string, which will check the string. An example is the following string
sampleString = "C123 A091 A111 A122 B120 B309 C000"
and examples of conditional string I have is as follows
example 1 :
A123 + B123 would I generate as
if sampleString.contains ("A123") And sampleString.contains ("B123") Then
'doSomething
else
'doSomething
end if
example 2 :
A111+A122+(B120/-C123)
if sampleString.contains ("A111") And sampleString.contains ("A122") And (sampleString.contains ("B120") Or Not sampleString.contains ("C123")) Then
'doSomething
else
'doSomething
end if
Plus (+) means AND
Minus (-) means NOT
Slash (/) means OR
Will I be able to do this in VB.Net?
Use Regex to solve the Contains problem by finding the matches and replace them with 1 or 0.
Dim sample As String = "C123 A091 A111 A122 B120 B309 C000"
Dim input As String = "A111 + A122 + ( B120/- C123)"
Dim nRegex As New Regex("\w+", RegexOptions.IgnoreCase)
Dim nMatches As MatchCollection = nRegex.Matches(input)
For Each nMatch As Match In nMatches
input = input.Replace(nMatch.Value, IIf(sample.Contains(nMatch.Value), "1", "0"))
Next
Now replace the special characters you have with ones that can actually give you the right result when evaluated as a math expression (Since logic is originally math).
input = input.Replace(" ", "")
input = input.Replace("+", "*")
input = input.Replace("/", "+")
input = input.Replace("-1", "0").Replace("-0", "1")
after all this you will get this expression :
1*1*(1+0)
You can now use any math evaluator as suggested in this Answer. If the result you get is bigger than zero means Its logically True. Otherwise False

check if a string's first 16 char is a number

I'm storing some files in database which has filename like 1839341255115211butterflies.jpg.I need to show this filename to the user as butterflies.jpg.I need to remove the first 16 digit and then show the filename.Added to it I also have few filenames which don't have this 16digit addition prior to the filename.Now my question is how do I identify if this string has 16digit numeric value prior to the filename, based on it remove the 1st 16digit and display just the filename. I'm aware of how to remove the first 16digit and retrive the filename but need help on how to identify a string that has 16digit.
Any suggestion is much appreciated.
A regular expression looks like a good fit here:
^[0-9]{16}
The above will match on strings that start with 16 digits (0 to 9).
Usage:
if(Regex.Match(fileName, #"^[0-9]{16}").Success)
{
fileName = fileName.Remove(0, 16);
}
string.Remove will work quite nicely:
var str = "1839341255115211butterflies.jpg";
str = str.Remove(0, 16);
Console.WriteLine(str);
With Linq:
remove all digits at the beginning until 16 digits:
string file = "1839341255115211butterflies.jpg";
string extension = Path.GetExtension(file);
string fileName = Path.GetFileNameWithoutExtension(file);
fileName = new string(fileName.Where((c, i) => i >= 17 || !Char.IsDigit(c)).ToArray());
file = fileName + extension;
Demo
Edit: If you just want to know if the first 16 chars are digits, it's easier and more readable:
bool startsWith16Digits = file.Take(16).All(Char.IsDigit);

Formatting a string

I have got a string in the form:
8 AM;10:15 AM;3:30 PM;6:15 PM
which i need to format in the form
8 AM-10:15 AM, 3:30 PM-6:15 PM
How can i achieve this?
You could do string splitting on ;, like this
var parts = s.Split(";".ToCharArray());
Then you could use the parts, like this:
var result = string.Format("{0}-{1}, {2}-{3}", parts);
This requires that the string always contains 4 parts.

How to write custom regular expression

I Would like to write a custom regular expression where the format is like XYZ-ABC-001
where
XYZ is fixed,
ABC is variable (it can be any three characters between A-Z )
001 is variable (it can be any number between 0-9)
This will match:
XYZ-[a-zA-Z]{3}-\d+
DIY kit: http://txt2re.com/
Example: http://txt2re.com/index-csharp.php3?s=XYZ-ABC-001&4&22&23&24&-17&-18&14&15&16
Results in the C# output:
string txt="XYZ-ABC-001";
string re1="((?:[a-z][a-z]+))"; // Word 1
string re2="(-)"; // Any Single Character 1
string re3="(.)"; // Any Single Character 2
string re4="(.)"; // Any Single Character 3
string re5="(.)"; // Any Single Character 4
string re6="(-)"; // Any Single Character 5
string re7="(\\d)"; // Any Single Digit 1
string re8="(\\d)"; // Any Single Digit 2
string re9="(\\d)"; // Any Single Digit 3
Regex r = new Regex(re1+re2+re3+re4+re5+re6+re7+re8+re9,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String word1=m.Groups[1].ToString();
String c1=m.Groups[2].ToString();
String c2=m.Groups[3].ToString();
String c3=m.Groups[4].ToString();
String c4=m.Groups[5].ToString();
String c5=m.Groups[6].ToString();
String d1=m.Groups[7].ToString();
String d2=m.Groups[8].ToString();
String d3=m.Groups[9].ToString();
Console.Write("("+word1.ToString()+")"+"("+c1.ToString()+")"+"("+c2.ToString()+")"+"("+c3.ToString()+")"+"("+c4.ToString()+")"+"("+c5.ToString()+")"+"("+d1.ToString()+")"+"("+d2.ToString()+")"+"("+d3.ToString()+")"+"\n");
}
Then you can modify it slightly to adjust to your needs, for example replacing (.) with ([A-Z]).

Resources