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
:: rotation facing mouse unity 
Csharp ::  
Csharp :: how to delay between lines in unity 
Csharp :: unity read from text file 
Csharp :: wpf update listview itemssource 
Csharp :: convert string to array c# 
Csharp :: c# console play sound 
Csharp :: c sharp if string equals 
Csharp :: c# class to byte array 
Csharp :: debug.log unity 
Csharp :: unity print name of button when click on it 
Csharp :: bubble sort in c# 
Csharp :: c# choose first n elements from list 
Csharp :: c# unity follow object 
Csharp :: c# dictionary first 
Csharp :: mvc list to jsonresult 
Csharp :: start command line from c# 
:: c# convert string to int 
Csharp :: unity button press onclick click add C# 
Csharp :: unity random 
Csharp :: unity check if a animator parameter trigger is activated 
Csharp :: 2d game art 
Csharp :: c# string.join 
Csharp :: placeholder syntax c# 
Csharp :: unity new input system keydown 
Csharp :: enum element count C# 
Csharp :: c# compress string 
Csharp :: c# join string array 
Csharp :: list all files in directory and subdirectories c# 
Csharp :: random.range unity not working 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =