// This is ported from the CDK and modified to not do so much uneccessary looping public static string GetString(IMolecularFormula formula) { var carbon = new Element("C"); var poossibleElements = MolecularFormulaManipulator.containsElement(formula, carbon) ? HillSystem.ElementsWithCarbons : HillSystem.ElementsWithoutCarbons; var elemCounts = new OrderedDictionary(); foreach (var possibleElement in poossibleElements) { elemCounts.Add(possibleElement, 0); } foreach (var isotope in formula.isotopes().ToWindowsEnumerable<IIsotope>()) { var isotopeSymbol = isotope.getSymbol(); var currentCount = (int)elemCounts[isotopeSymbol]; elemCounts[isotopeSymbol] = currentCount + formula.getIsotopeCount(isotope); } var parts = new List<string>(); foreach (DictionaryEntry elemCount in elemCounts) { var count = (int)elemCount.Value; var elem = (string)elemCount.Key; if (count == 1) { parts.Add(elem); } else if (count > 1) { parts.Add(string.Format("{0}{1}", elem, count)); } } return string.Join("", parts); }