Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 string variables as below

string name = "firstname|lastname" ;
string value = "Vishal|Lamba" ;

I need to assign first name = "Vishal" and lastname = "Lamba"

value and names in pipe separated string may increase dynamically

How do I do this In c# without using dictionary as I need each variables for separate operations

What I have tried:

-------------------------clueless---------------------------
Posted
Updated 16-Sep-20 21:46pm
Comments
PIEBALDconsult 16-Sep-20 14:59pm    
I'd still use a Dictionary, I don't understand why you can't.
What are you actually trying to accomplish.

 
Share this answer
 
Quote:
How do I do this In c# without using dictionary as I need each variables for separate operations

Use an array for dynamic number of substring.
To do it, try to split the string as suggested by Richard in S1.
 
Share this answer
 

I am not exactly sure what you require but this example may be helpful.


C#
StringBuilder sb = new StringBuilder();
for (int i = 0; i <20; i++)
{
 sb.Append($" F{i}|S{i}");
}
//build a test string where the format is 'forename0|surname0 forename1|surname1 ..'
string testString = sb.ToString().Trim();
//use Linq to get an enumerable of 2 element string arrays 
var names = testString.Split(' ').Select(n => n.Split('|'));
//For ease of use, convert into a named value tuple list 
var tuples = names.Select(n => (n[0], n[1])).ToList<(string ForeName, string SurName)>();
foreach (var t in tuples)
{
 Console.WriteLine($"ForeName= {t.ForeName} SurName= {t.SurName}");
}
Console.ReadLine();
 
Share this answer
 

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