Reputation: 124
Let's say that I have a string that has the following:
string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";
and I want to get each of the substrings that have the'*' and then add them to a new string and then get each of the values that have the ':' same with the one that has '-', and so I tried this using the split method, but it does not work.
string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";
string[] myid = mystr.Split('*');
string[] myemail = mystr.Split('-');
string[] mypwd = mystr.Split(':');
string ids = "";
string emails = "";
string pwds = "";
foreach (string k in myid)
{
Console.WriteLine("THESE ARE THE IDS");
Console.WriteLine(k);
}
foreach (string k in myemail)
{
Console.WriteLine("THESE ARE THE EMAILS");
Console.WriteLine(k);
}
foreach (string k in mypwd)
{
Console.WriteLine("THESE ARE THE PASSWORDS");
Console.WriteLine(k);
}
Here is the console output:
THESE ARE THE IDS
THESE ARE THE IDS
myid
THESE ARE THE IDS
-myemail-:mypwd:
THESE ARE THE IDS
myid2
THESE ARE THE IDS
-myemail2-:mypwd2:
THESE ARE THE IDS
myid3
THESE ARE THE IDS
-myemail3-:mypwd3:
THESE ARE THE EMAILS
*myid*
THESE ARE THE EMAILS
myemail
THESE ARE THE EMAILS
:mypwd:*myid2*
THESE ARE THE EMAILS
myemail2
THESE ARE THE EMAILS
:mypwd2:*myid3*
THESE ARE THE EMAILS
myemail3
THESE ARE THE EMAILS
:mypwd3:
THESE ARE THE PASSWORDS
*myid*-myemail-
THESE ARE THE PASSWORDS
mypwd
THESE ARE THE PASSWORDS
*myid2*-myemail2-
THESE ARE THE PASSWORDS
mypwd2
THESE ARE THE PASSWORDS
*myid3*-myemail3-
THESE ARE THE PASSWORDS
mypwd3
THESE ARE THE PASSWORDS
And this is what I want it to look like:
THESE ARE THE PASSWORDS
mypwd
mypwd2
mypwd3
THESE ARE THE EMAILs
myemail
myemail2
myemail3
THESE ARE THE IDS
myid
myid2
myid3
Upvotes: 0
Views: 148
Reputation: 19641
You may use Regular Expressions for this:
string mystr = "*myid*-myemail-:mypwd:*myid2*-myemail2-:mypwd2:*myid3*-myemail3-:mypwd3:";
var matches = Regex.Matches(mystr, @"\*(?<Id>[^*]+)\*-(?<Email>[^-]+)-:(?<Password>[^:]+):");
Then, you can either access the matches as follows:
foreach (Match match in matches)
{
Console.WriteLine("Id=" + match.Groups["Id"].Value);
Console.WriteLine("Email=" + match.Groups["Email"].Value);
Console.WriteLine("Password=" + match.Groups["Password"].Value);
}
Or if you want to group the Ids, emails, and passwords together, use:
var ids = matches.OfType<Match>().Select(m => m.Groups["Id"].Value);
var emails = matches.OfType<Match>().Select(m => m.Groups["Email"].Value);
var passwords = matches.OfType<Match>().Select(m => m.Groups["Password"].Value);
foreach (string id in ids)
Console.WriteLine(id);
foreach (string email in emails)
Console.WriteLine(email);
foreach (string password in passwords)
Console.WriteLine(password);
Note, however, that this might break because an email address could contain a hyphen character and the password could contain a colon. In that case, you need to come up with a more robust format for the input text.
Upvotes: 2