How to Split String into three parts in C# - asp.net

I have a string like "123456 abcdefgh ijkl mno 78903 "
Now, I have to split into three parts i.e
string1- "123456"
string2 - "abcdefgh ijkl mno"
string3 - "78903"

So your common splitter is a space, which essencially lets you do this:
public void Example()
{
var content = "123456 abcdefgh ijkl mno 78903";
var parts = content.Split(' '); // split on spaces
var string1 = parts.First();
var string3 = parts.Last();
var theRest = content
.Replace(parts.First(), "")
.Replace(parts.Last(), "");
}
What I basically did over here is to split the original string on space, so I have an array of parts, then I get the first and the last part, and finally, I remove those from the content to provide what is left.

Related

splitting the string that has more than or equal to two white spaces

I have a string. I want to split the string on uneven white spaces. if the white spaces are more or equal to 2 spaces in length than I want them to be in seperate array item, but if there is only one space then I want them to be in same array item so for e.g.
I have this string
1234 This is a Test PASS 1255432 12/21/2016 07:14:11
so when I split the above string, it should be like this
arr(0) = 1234
arr(1) = This is a test ' because it has only one space in between, it there are more or equal to two spaces than I want it to be a seperate item in an array
arr(2) = Pass
arr(3) = 1255432
arr(4) = 12/21/2016
arr(5) = 07:14:1
same thing with below string:
0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16
When I split the above string, it should like this:
arr(0)=0001
arr(1) = This is a Marketing text_for the students
arr(2) = Test2
arr(3)=468899
arr(4)=12/23/2016
arr(5) = 06:23:16
Is there any regular expression that can help me to split the string based on spaces, but put together the words if the space is more or equal to 2.
Any help will be greatly appreciated.
This can be done with this regex (\s{0,1}\S+)+ like so:
string text = "0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16";
Regex regex = new Regex(#"(\s{0,1}\S+)+");
var matches = regex.Matches("0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16").Cast<Match>()
.Select(m => m.Value)
.ToArray();
Console.WriteLine(String.Join( ",", matches));
This is a working java Script snippet of the same thing.
var value = "0001 This is a Marketing text_for the students TEST2 468899 12/23/2016 06:23:16";
var matches = value.match(
new RegExp("(\\s{0,1}\\S+)+", "gi")
);
console.log(matches)
This regex (\s{0,1}\S+)+ works by matching 0 or 1 spaces at the begging of each match with \s{0,1} and then any number of things that aren't a space with \S+ it then matches this whole thing any number of times by containing it in parentheses and using the + operator (...)+, this allows for the string together of single spaces characters.

Excel Delimiter Splitting

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);

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);

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