您好,欢迎访问一九零五行业门户网

如何将哈希表转换为字符串?

在c#中,hashtable集合是一个非泛型的元素集合。hashtable的每个元素都表示为一个键值对。hashtable的键是非空且唯一的。值可以重复和/或为空。
c# systems 的 hashtable 类。集合接口是 hashtable 集合的表示。该类提供了各种构造函数、方法和属性来操作 hashtable 集合。
我们还可以将哈希表转换为其他集合,如数组、arraylist等,还可以转换为字符串表示。
在本文中,让我们讨论如何将哈希表集合转换为字符串。
如何将哈希表项转换为字符串?请注意,要将哈希表的项转换为字符串,哈希表类不像java那样提供直接的方法。也没有内置属性来进行转换。因此,我们需要设计自己的方法来进行转换。
哈希表元素转换为字符串表示形式。为此,我们必须遍历哈希表集合,然后将每个项目的键和值复制到字符串中。我们可以使用分隔符来分隔字符串中的每个键值项。
要实际使用 c# 进行编程,我们使用 idictionaryenumerator 来迭代哈希表中的每个项目。枚举器保存哈希表的每个项目(键和值)。然后,我们可以使用循环将每个键和值复制到字符串中,如下所示。
idictionaryenumerator enumerator = phonetics.getenumerator();while (enumerator.movenext()) { text += enumerator.key + , ; text += enumerator.value + ;}
正如我们在上面的代码中看到的,将每个键值对复制到字符串后,我们插入一个分隔符“
”,以便分隔每个项目。
示例下面是将散列表项转换为字符串的完整c#程序。
using system;using system.collections;class myhashtable { public static void main() { // create a hashtable named phonetics hashtable phonetics = new hashtable(); // add key/value pairs in phonetics phonetics.add(a, apple); phonetics.add(b, banana); phonetics.add(c, cat); phonetics.add(d, dog); phonetics.add(e, elephant); phonetics.add(f, fish); //print hahshtable collection console.writeline(hashtable items:); foreach(dictionaryentry entry in phonetics){ console.writeline({0} and {1} , entry.key, entry.value); } string text = ; idictionaryenumerator enumerator = phonetics.getenumerator(); while (enumerator.movenext()) { text += enumerator.key + , ; text += enumerator.value + ; } console.writeline(string value: {0}, text); }}
在这个程序中,我们有一个从a到f的音标哈希表。我们声明一个字符串变量'text'。然后我们声明一个idictionaryenumerator来遍历哈希表。然后使用这个枚举器,我们使用一个'while'循环将哈希表集合的内容复制到字符串中。
输出程序生成以下输出。
hashtable items:b and banana c and cat a and apple f and fish d and dog e and elephant string value: b, bananac, cata, applef, fishd, doge, elephant
在这里,我们首先显示了哈希表集合的内容。接下来,我们显示了字符串。我们可以看到字符串值由哈希表的所有元素以‘
’(换行符)分隔而成。
示例让我们实现另一个程序,将哈希表转换为字符串。完整的c#程序如下所示。
using system;using system.collections;class myhashtable { public static void main() { // create a hashtable named numbernames hashtable numbernames = new hashtable(); // add key/value pairs in numbernames numbernames.add(10, ten); numbernames.add(20, twenty); numbernames.add(30, thirty); numbernames.add(40, forty); numbernames.add(50, fifty); numbernames.add(60, sixty); //print hahshtable collection console.writeline(hashtable items:); foreach(dictionaryentry entry in numbernames){ console.writeline({0} => {1} , entry.key, entry.value); } string text = ; idictionaryenumerator enumerator = numbernames.getenumerator(); while (enumerator.movenext()) { text += enumerator.key + and ; text += enumerator.value + ; } console.writeline(string value: {0}, text); }}
这个程序与之前的程序类似,只是在显示哈希表和字符串值的方式上有一些细微的变化。
在这里,我们有一个以数字为键,以其对应的数字名称为值的哈希表。然后,我们使用idictionaryenumerator声明一个遍历哈希表的对象,并将哈希表的每个项(键和值)复制到一个字符串中。每个哈希表项之间用分隔符‘
’分隔。
输出程序生成以下输出。
hashtable items:10 => ten 60 => sixty 40 => forty 50 => fifty 20 => twenty 30 => thirty string value: 10 and ten60 and sixty40 and forty50 and fifty20 and twenty30 and thirty
从上面的输出中我们可以看到,哈希表集合项已成功复制到字符串变量。
因此,在本文中,我们讨论了将哈希表集合转换为字符串。尽管没有直接的方法来执行此转换,但我们可以使用以下命令轻松地将哈希表集合转换为字符串:idictionaryenumerator。它保存了哈希表的项目(键和值),我们可以轻松访问这些项目并使用循环将其复制到字符串中。
以上就是如何将哈希表转换为字符串?的详细内容。
其它类似信息

推荐信息