here is my problem
I have the following array (for example)
string[] arr = new[] { "s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007" };
I want to do something that gives the following output
s_0001
sa_0004
sab_0006
I’ve tried everything but no luck! this will be the first step in a long project and any help would be most appreciated.
[edit] I don’t know when will the letters change, but I know that there will always be an underscore to separate the letters from the numbers. I need to somehow extract these letters, and then get rid of the duplicate ones
[edit] More specifically.. I wanna have unique entries of each string before the underscore, the numbers I don’t care about
[edit]
Ok guys! You’re really active I give you that. I didn’t expect I would get such quick answers. But as it seems (since I’ve been working on this for the last 8 hours) I’ve asked the wrong question
Here is my code
//Loop through the XML files in the Directory and get
//the objectName and GUID of each file
string[] arr_xmlFiles = Directory.GetFiles(Dir, "*.xml"); //Array with all XML Files in the Directory
foreach (string xmlFile in arr_xmlFiles)
{
try
{
//Get the XMLs Name
XDocument xmlF = XDocument.Load(xmlFile);
string objectName = xmlF.Root.Name.ToString();
//Get the XMLs GUID
XElement oDcElement = xmlF.Root.FirstNode as XElement;
Guid oGuid = new Guid(oDcElement.Attribute("DataclassId").Value);
//Prints out the results
Console.WriteLine(" " + objectName + " " + oGuid);
}
catch (XmlException) { }
}
What I’m doing basically is the following
I get all the XML files in a directory (They contain the ObjectName with its GUID)
i.e
CM_Commands [0ee2ab91-4971-4fd3-9752-cf47c8ba4a01].xml
CM_Commands [1f627f72-ca7b-4b07-8f93-c5750612c209].xml
Sorry the breaking sign was ‘[‘ not ‘_’ but it doesn’t matter.
Now I save all these XMLs in an Array, then I wanna extract from these XMLs the ObjectName and the GUID for each one
After I do that I wanna do some modifications on only one of each XML that holds the same objectName
That’s all
EDIT #3: detailed comments added to snippet below (see updated code under EDIT 2). Also note that if you want to return these from a method you’ll need to setup a new class with these properties, such as:
With a class available, the select statement would change from
select new { ... }to:Your method, with all this code, would then have a return type of
IEnumerable<MyClass>. You could easily change it to aList<MyClass>by usingreturn results.ToList();.EDIT #2: to extract the objectName and Guid from your filename you don’t need to do all that tedious XML work to get the information from the internal details.
Assuming your
objectNameandGuidare always separated by a space, you can use the following code. Otherwise more parsing (or, optionally, a regex) may be needed.This fits your sample data, however if you anticipate filenames with
.characters the above will break. To remedy such a scenario change:Splitto:let split = f.Name.Split(' ')Guid = split[1].Substring(0, split[1].LastIndexOf('.')),Since you know there’ll always be an underscore you can try this approach:
This will take the first item of each group so unless your items are pre-sorted, you may want to throw in an
OrderByin theSelect:.Select(g => g.OrderBy(s => s).First());EDIT: in response to your edit, to get the distinct letters before the underscore (i.e., s, sa, sab) you can use the Enumerable.Distinct method as follows:
That will give you an
IEnumerable<string>that you can iterate over with aforeachas shown earlier.