如何将字符串分成多个部分C# [英] How to split the string in multiple parts C#

查看:119
本文介绍了如何将字符串分成多个部分C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个字符串,需要将其拆分为多个部分.

字符串strcode:"qwer-tyui-opas-dfghdineshkumardora@yahoo.com08032018"

输出应为:

Invitationcode ="qwer-tyui-opas-dfgh"
emailIID ="dineshkumardora@yahoo.com"
日期="08032018"

我能够检索邀请代码和日期,但在提取电子邮件ID时遇到问题

我尝试过的事情:

邀请码= strcode.substring(0,19)
date = strcode.substring((strcode.length-8),8)

需要提取电子邮件ID.

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.

推荐答案

如果您知道邀请代码的前19个字符和数据的后8个字符,那么电子邮件始终是其余的''是吗?

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

不对,是:
/*正确*/
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*/
strcode.Substring(19, (strcode.Length - 27));



单元测试在这里:



unit test here:

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




在您的字符串之间添加一个标识符,然后使用它来拆分成一个数组.如何将字符串设置为:

Hi,

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

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:

string[] words = s.Split(';'); // words will have all three strings.



有关更多详细信息,请检查:
http://www.dotnetperls.com/split [



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


尝试一下

try this

string invitecode = strcode.Substring(0, 19);
       string date = strcode.Substring(strcode.Length - 8);
       string emailIID = strcode.Replace(invitecode, "").Replace(date, "");


这篇关于如何将字符串分成多个部分C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆