使用Dictionary键值对判断字符串中字符出现次数

介绍Dictionary

使用前需引入命名空间 using System.Collections.Generic

Dictionary里面每一个元素都是一个键值对(由两个元素组成:键和值)

键必须是唯一的,而值不需要唯一

键和值都可以是任何类型(比如:string,int,自定义类型等)

通过一个键读取一个值的时间接近0(1)

键值对之间的偏序可以不定义

使用Dictionary

使用dictionary判断字符串中字符出现次数

var dic = new Dictionary<char, int>();
string str = "welcome to china,and bei jing huan ying ni";
for (int i = 0; i < str.Length; i++)
{
    if (!dic.ContainsKey(str[i]))//判断是否包含指定键名
    {
        dic.Add(str[i], 1);
    }
    else
    {
        dic[str[i]]++;//如果这个键值对中存在这个元素,就把个数加1
    }
}
//利用一个键值对KeyValuePair来访问这个元素
foreach (KeyValuePair<char, int> item in dic)
{
    Console.WriteLine("字符{0} {1}在这个句子中出现了{2}次", item.Key, (int)item.Key, item.Value);
}
Console.ReadKey();

键值对中value是数组

//键值对中value是数组
var dic = new Dictionary<string, string[]>();
string[] HuBei = { "wuhan", "tianmen", "xiantao" };
string[] GuangDong = { "guangzhou", "shenzhen", "foshan" };
dic.Add("HB", HuBei);
dic.Add("GD", GuangDong);
Console.WriteLine(dic["HB"][0]);
Console.WriteLine(dic["GD"][1]);
Console.ReadKey();

键值对中value是类

static void Main(string[] args)
{
    //键值对中value是类
    var stuList = new Dictionary<string, Student>();
    for (int i = 0; i < 3; i++)
    {
        Student stu = new Student();
        stu.Num = i.ToString();
        stu.Name = "Student" + i.ToString();
        stuList.Add(i.ToString(), stu);
    }
    foreach (var student in stuList)
    {
        Console.WriteLine("Output : Key {0}, Num : {1}, Name : {2}",
        student.Key, student.Value.Num, student.Value.Name);
    }
    Console.ReadKey();
}

public class Student
{
    public string Num { get; set; }
    public string Name { get; set; }
}

End!

原文地址:https://www.cnblogs.com/gygg/p/11609166.html

时间: 2024-07-29 20:41:24

使用Dictionary键值对判断字符串中字符出现次数的相关文章

判断字符串中字符出现次数

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>txt</title> <style> p.i{ letter-spacing: 2px; } </style> </head> <body> <p></p> <script>

python 判断字符串中字符类型的常用方法

s为字符串 s.isalnum() 所有字符都是数字或者字母 s.isalpha() 所有字符都是字母 s.isdigit() 所有字符都是数字 s.islower() 所有字符都是小写 s.isupper() 所有字符都是大写 s.istitle() 所有单词都是首字母大写,像标题 s.isspace() 所有字符都是空白字符. .. 判断是整数还是浮点数 a=123 b=123.123 >>>isinstance(a,int) True >>>isinstance(

【c++程序】统计字符串中字符出现次数

#include<iostream> #include<string> //#include<cstring> using namespace std; int main() { string str; cout<<"input some text:"<<endl; getline(cin,str); //char str[200]; //cin.getline(str,200); int cnt[256]={}; for(i

CareerCup之1.1字符串中字符判重

[题目] Chapter 1 | Arrays and Strings 原文: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 译文: 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) [分析] [思路一]首先,我们要搞清

C#判断字符串是否存在字母及字符串中字符的替换实例

本文实例讲述了C#判断字符串是否存在字母及字符串中字符的替换的方法.分享给大家供大家参考.具体实现方法如下: 首先要添加对命名空间"using System.Text.RegularExpressions;"的引用 下面以一个字符串为例: 代码如下: string ss = "aaZ31 dd2f3"; string sss = ss.Replace(" ", "");//将字符串ss中的空格去掉 string sss2 =

Node.js之判断字符串中是否包含某个字符串

server.txt内容如下: 阿里云服务器 关于应用场景,就不多说了,字符串是不论是后端开发还是前端开发等,都是要经常打交道了. test.js(node.js代码,只要被本地装了node.js环境,直接可通过node test.js运行看效果): var fs = require("fs"); var result = fs.readFileSync("./server.txt"); console.log("result:"+result)

PHP判断字符串中是否包含指定字符串,支持中文哦

RT,随手写的 1 /** 2 * 判断字符串中是否包含指定字符串 3 * @var source 源字符串 4 * @var target 要判断的是否包含的字符串 5 * @return bool 6 */ 7 function hasstring($source,$target){ 8 preg_match_all("/$target/sim", $source, $strResult, PREG_PATTERN_ORDER); 9 return !empty($strResul

iOS 判断字符串中含有某个字符串rangeOfString

//_roaldSearchText if([roadTitleLab.text rangeOfString:@"格力"].location !=NSNotFound) NSLog(@"yes"); else NSLog(@"no"); iOS 判断字符串中含有某个字符串rangeOfString,布布扣,bubuko.com

PHP判断字符串中是否含有中文

<?php $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) { //只能在GB2312情况下使用 //if (preg_match("/^[\x7f-\xff]+$/", $str)){ //兼容gb2312,utf-