Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# list to string

List<string> names = new List<string>() { "John", "Anna", "Monica" };
string result = string.Join(",", names.ToArray());
Comment

c# convert list to string

string combindedString = string.Join( ",", myList.ToArray() );
Comment

list of string to string c#

string Something = string.Join(",", MyList);
Comment

c# list to string

using System.Linq;
string str = list.Aggregate((x, y) => x + ',' + y);
Comment

c# convert list to string and back

public static class UsersConverter
{
    // Separates user properties.
    private const char UserDataSeparator = ',';

    // Separates users in the list.
    private const char UsersSeparator = ';';

    public static string ConvertListToString(IEnumerable<User> usersList)
    {
        var stringBuilder = new StringBuilder();

        // Build the users string.
        foreach (User user in usersList)
        {
            stringBuilder.Append(user.Name);
            stringBuilder.Append(UserDataSeparator);
            stringBuilder.Append(user.Age);
            stringBuilder.Append(UsersSeparator);
        }

        // Remove trailing separator.
        stringBuilder.Remove(stringBuilder.Length - 1, 1);

        return stringBuilder.ToString();
    }

    public static List<User> ParseStringToList(string usersString)
    {
        // Check that passed argument is not null.
        if (usersString == null) throw new ArgumentNullException("usersString");

        var result = new List<User>();

        string[] userDatas = usersString.Split(UsersSeparator);

        foreach (string[] userData in userDatas.Select(x => x.Split(UserDataSeparator)))
        {
            // Check that user data contains enough arguments.
            if (userData.Length < 2) throw new ArgumentException("Users string contains invalid data.");

            string name = userData[0];
            int age;

            // Try parsing age.
            if (!int.TryParse(userData[1], out age))
            {
                throw new ArgumentException("Users string contains invalid data.");
            }

            // Add to result list.
            result.Add(new User { Name = name, Age = age });
        }

        return result;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity player look at mouse 
Csharp :: unity application quit 
Csharp :: get absolute url c# 
Csharp :: Program for factorial of a number in c# 
Csharp :: get scene name unity 
Csharp :: loop through enum c# 
Csharp :: how to change a image with code unity 
Csharp :: dotnet executable directory 
Csharp :: unity change text color 
Csharp :: unity wait for seconds 
Csharp :: unity Protected 
Csharp :: how to make a method wait in unity 
Csharp :: unity change play mode color 
Csharp :: c# how to get screen resolution in class 
Csharp :: 2d unity point at 
Csharp :: c# absolute value 
Csharp :: unity remove gameobject 
Csharp :: c# socket bind to localhost 
Csharp :: move file c# 
Csharp :: substring c# after character 
Csharp :: month number to text in c# 
Csharp :: c# exit 
Csharp :: website link in unity 
Csharp :: disable script in unity 
Csharp :: unity particle system playing at the wrong location 
Csharp :: c# take only int from string 
Csharp :: index in foreach c# 
Csharp :: remove whitespace between string c# 
Csharp :: removing illlegal char from filename 
Csharp :: unity agent does not move 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =