问题描述
您好,我有一个字符串,需要将其拆分为多个部分.
字符串strcode:"[email protected]"
输出应为:
Invitationcode ="qwer-tyui-opas-dfgh"
emailIID ="[email protected]"
日期="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: "[email protected]"
Output should be:
invitecode = "qwer-tyui-opas-dfgh"
emailIID = "[email protected]"
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.
推荐答案
strcode.Substring(19, (strcode.Length - 27));
单元测试在这里:
unit test here:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string strcode = "[email protected]";
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("[email protected]", email);
Assert.AreEqual("08032018", date);
}
}
string strcode: "qwer-tyui-opas-dfgh;[email protected];08032018"
or
string strcode: "qwer-tyui-opas-dfgh%[email protected]%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[^]
string invitecode = strcode.Substring(0, 19);
string date = strcode.Substring(strcode.Length - 8);
string emailIID = strcode.Replace(invitecode, "").Replace(date, "");
这篇关于如何将字符串分成多个部分C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!