get value after chararray

I have a string. I am spliting the string using
NewLine.ToCharArray()
Now I have the count of each row and I have the value.

I want to retrieve the value and make the value the object name and everything after the equal sign is the value.
can this be accomplish.

example
		
Name     value
[1]	"A2='Founndowment"	string
[26]	"B9='-0.0377"	string
[35]	"C11='4245497.16"	string

to

Name     value
A2       Founndowment 
B9       -0.0377
c11      4245497.16

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Can you post the actual code you are using to split the strings? Is the Strings in a List<String> or an Array? Or A custom collection with properties Name and Value?

How do you want the values returned as. Please state your requirements. Thanks.
Seven priceFull StackAuthor Commented:
string output
 var sFileLines = output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None);

Open in new window

Seven priceFull StackAuthor Commented:
I try to put a for loop after to return only the values then split the value to key and value but did not work.
 foreach (string entry in sFileLiness)
            {
                var dict = output.Split('=')
   .Select(x => x.Split('='))
   .Where(x => x.Length > 1 && !String.IsNullOrEmpty(x[0].Trim())
    && !String.IsNullOrEmpty(x[1].Trim()))
   .ToDictionary(x => x[0].Trim(), x => x[1].Trim());
            }
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Seven priceFull StackAuthor Commented:
I've requested that this question be deleted for the following reason:

never mind I figured it out.
var sFileLiness = output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(new[] { '=' })); ;
         
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var item in sFileLiness)
            {
                dict.Add(item[0], item[1]);
            }
Fernando SotoRetiredCommented:
Always like spinning my wheels.
it_saigeDeveloperCommented:
Snake you could take it a step further; e.g. -
var sFileLiness = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(pairs => pairs.Split('=')).ToDictionary(k => k[0], v => v[1]);

Open in new window

Proof of concept -
using System;
using System.Linq;

namespace EE_Q28837000
{
	class Program
	{
		const string data = "A2='Founndowment\r\nB9='-0.0377\r\nC11='4245497.16";

		static void Main(string[] args)
		{
			var sFileLiness = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(pairs => pairs.Split('=')).ToDictionary(k => k[0], v => v[1]);
			foreach (var pair in sFileLiness)
				Console.WriteLine("Key: {0}; Value: {1}", pair.Key, pair.Value);
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-
Seven priceFull StackAuthor Commented:
Ok what if there is a null or no value just object
Seven priceFull StackAuthor Commented:
Index was outside the bounds
it_saigeDeveloperCommented:
Great question.  In that case you would simply add logic to the value portion of the ToDictionary selector so that it would check to ensure:
A.  There are two index's in the array.
B.  The value of the second index is not null

If either of the above fails, then you simply create a default value for your value, in this case, I used an empty string; e.g. -
using System;
using System.Linq;

namespace EE_Q28837000
{
	class Program
	{
		const string data = "A2='Founndowment\r\nB9='-0.0377\r\nC11='4245497.16\r\nD16";

		static void Main(string[] args)
		{
			var sFileLiness = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(pairs => pairs.Split('=')).ToDictionary(k => k[0], v => v.Length == 2 && v[1] != null ? v[1] : string.Empty);
			foreach (var pair in sFileLiness)
				Console.WriteLine("Key: {0}; Value: {1}", pair.Key, pair.Value);
			Console.ReadLine();
		}
	}
}

Open in new window

Which now produces the following output -Capture.JPGYou could also use Count instead of Length (seems more natural that way):
using System;
using System.Linq;

namespace EE_Q28837000
{
	class Program
	{
		const string data = "A2='Founndowment\r\nB9='-0.0377\r\nC11='4245497.16\r\nD16";

		static void Main(string[] args)
		{
			var sFileLiness = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(pairs => pairs.Split('=')).ToDictionary(k => k[0], v => v.Count() == 2 && v[1] != null ? v[1] : string.Empty);
			foreach (var pair in sFileLiness)
				Console.WriteLine("Key: {0}; Value: {1}", pair.Key, pair.Value);
			Console.ReadLine();
		}
	}
}

Open in new window

Which would produce the same output as show in this example.

-saige-
Seven priceFull StackAuthor Commented:
Ok last question then I will close. What about duplicates. because I am running into that. Can I filter them out.
it_saigeDeveloperCommented:
Yes you can (I am assuming you mean keys), for that though, we want to group the pairs by their keys before selecting into the dictionary:
using System;
using System.Linq;

namespace EE_Q28837000
{
	class Program
	{
		const string data = "A2='Founndowment\r\nB9='-0.0377\r\nC11='4245497.16\r\nD16\r\nA2='Belvadere";

		static void Main(string[] args)
		{
			var sFileLiness = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(pairs => pairs.Split('=')).GroupBy(pair => pair[0], pair => pair.Count() == 2 ? pair[1] : string.Empty).ToDictionary(k => k.Key, v => v.FirstOrDefault());
			foreach (var pair in sFileLiness)
				Console.WriteLine("Key: {0}; Value: {1}", pair.Key, pair.Value);
			Console.ReadLine();
		}
	}
}

Open in new window

Which now produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
tks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.