Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a string which i need to split in various parts.

string strcode: "qwer-tyui-opas-dfghdineshkumardora@yahoo.com08032018"

Output should be:

invitecode = "qwer-tyui-opas-dfgh"
emailIID = "dineshkumardora@yahoo.com"
date = "08032018"

I am able to retrieve invite code and date but facing problem in extracting email id

What I have tried:

invitecode = strcode.substring(0,19)
date= strcode.substring((strcode.length-8),8)

email id needs to be extracted.
Posted
Updated 26-Aug-16 6:02am
Comments
F-ES Sitecore 26-Aug-16 9:01am    
You have the length of the first bit of text so the email starts after that. The email's length is then the length of the string-19-8, so do some maths and use Substring to get from position 19 for the length of the email

Well if you know that invite code is first 19 characters and data is last eight characters the email is always the rest isn't it?

/*wrong*/
email = strcode.substring(19, (strcode.length-8))

no sorry it is:
/*correct*/
C#
strcode.Substring(19, (strcode.Length - 27));


unit test here:
C#
[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string strcode = "qwer-tyui-opas-dfghdineshkumardora@yahoo.com08032018";
            var invitecode = strcode.Substring(0, 19);
            var date = strcode.Substring((strcode.Length - 8), 8);
            var email = strcode.Substring(19, (strcode.Length - 27));
            Assert.AreEqual("qwer-tyui-opas-dfgh", invitecode);
            Assert.AreEqual("dineshkumardora@yahoo.com", email);
            Assert.AreEqual("08032018", date);
        }
    }
 
Share this answer
 
v2
Hi,

Add a identifier between your strings and then use it to split into an array. How about setting your string as :

C#
string strcode: "qwer-tyui-opas-dfgh;dineshkumardora@yahoo.com;08032018" 
 or 
string strcode: "qwer-tyui-opas-dfgh%dineshkumardora@yahoo.com%08032018" 


Then using it like:
C#
string[] words = s.Split(';'); // words will have all three strings.


For more details check this :
http://www.dotnetperls.com/split[^]
 
Share this answer
 
try this

C#
string invitecode = strcode.Substring(0, 19);
       string date = strcode.Substring(strcode.Length - 8);
       string emailIID = strcode.Replace(invitecode, "").Replace(date, "");
 
Share this answer
 
You already have everything you need
C#
invitecode = strcode.substring(0,19)
date= strcode.substring((strcode.length-8),8)

First part start at 0 and length is 19 last char is 0+19-1 => 18
second part start after first and end before third part.
second part length is strcode.length-19-8

C#
emailIID= strcode.substring((19, strcode.length-19-8)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900