简体   繁体   English

根据 C# 中包含的字符为字符串赋值的简单方法是什么?

[英]What's an easy way to give a string a value based on the characters it contains in C#?

I'm trying to get a sum value based on what characters a string contains.我试图根据字符串包含的字符来获得总和值。 The character values are determined by me and are somewhat arbitrary ('A' = 1, 'B' = 4, 'C' = 2, etc.).字符值由我确定并且有些随意('A' = 1、'B' = 4、'C' = 2 等)。 For example, if string s = "ABC" then int value = 7 because 1 + 4 + 2 = 7. What would be an efficient way to write this in C#?例如,如果string s = "ABC" ,则int value = 7 ,因为 1 + 4 + 2 = 7。在 C# 中编写此代码的有效方法是什么?

Right now my code looks like this:现在我的代码如下所示:

//Declare variables
string name = JOHN
int i = 0;
int nameValue = 0;
string temp = "";
string[] nameArray;

//Convert name to string array so I can use String.Contains() to check what letters are in name
foreach (char c in name)
{
   temp += c.ToString();
   temp += ".";
}

temp = temp.Remove(temp.Length - 1, 1);
nameArray = temp.Split('.');            

//Determine nameValue by iterating through nameArray and checking each string
foreach (string s in nameArray)
{
   if (nameArray[i].Contains('A')) { nameValue += 1 }
   else if (nameArray[i].Contains('B')) { nameValue += 4 }
   else if (nameArray[i].Contains('C')) { nameValue += 2 }
   else if (nameArray[i].Contains('D')) { nameValue += 3 }
   .
   .
   .
   else if (nameArray[i].Contains('Y')) { nameValue += 7 }
   else if (nameArray[i].Contains('Z')) { nameValue += 5 }

   i++;
}

Console.WriteLine(nameValue);

I changed the string to a string array because I have names that have repeating letters (ie Jill), and I want to give every letter in the name a value.我将字符串更改为字符串数组,因为我的名称中有重复的字母(即 Jill),并且我想给名称中的每个字母一个值。 If I used String.Contains() without separating every letter, it would only count repeating letters once, I think.如果我使用 String.Contains() 而不分隔每个字母,我认为它只会计算重复字母一次。

I feel like there has to be a better way than doing all that string manipulation and using a separate conditional statement for every letter in the alphabet, but I couldn't find anything.我觉得必须有一种更好的方法,而不是进行所有字符串操作并为字母表中的每个字母使用单独的条件语句,但我找不到任何东西。 Thanks.谢谢。

If you want to map consequent characters (say, 'A'..'Z' ) to integer values, I suggest using array :如果您想将 map后续字符(例如'A'..'Z' )转换为 integer 值,我建议使用数组

  using System.Linq;

  ...

  int[] map = new int[] {
    //TODO: put corresponding integer values starting from 'A'
    4, 2, 3
  };  

  ...

  s = "ABC";

  int sum = s.Sum(c => map[c - 'A']);

In general case, if you want to map arbitrary symbols, you can use dictionary :一般情况下,如果要 map任意符号,可以使用字典

  using System.Linq;

  ...

  Dictionary<char, int> map = new Dictionary<char, int>() {
    //TODO: put all required pairs here
    {'A', 4},
    {'B', 2},
    {'C', 1},
    {'#', -123},
  };

  ...

  s = "ABC";

  // if there's no map (e.g. for '*'), we assume the value being 0
  int sum = s.Sum(c => map.TryGetValue(c, out int v) ? v : 0);   

http://www.asciitable.com/ http://www.asciitable.com/

you will see that capitol A is 65你会看到国会大厦 A 是 65

string str = "Hi there";
foreach(char c in str.ToUpper())
{
    value += ((int)c) - 64;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 中避免字符串添加的简单方法 - Easy way to avoid string addition in C# 在C#中获取string []子集的简单方法 - Easy way to get subset of string[] in C# 有没有一种简单的方法来在C#中更改字符串中的字符? - Is there an easy way to change a char in a string in C#? 在C#中删除字符串开头的字符的最佳方法是什么? - What is the best way to remove characters at start of a string in c#? 在C#中迭代字符串中单个字符的最快方法是什么? - What is the fastest way to iterate through individual characters in a string in C#? 在字符串C#末尾删除几个字符的正确方法是什么 - What is correct way to a remove few characters at the end of string C# 在C#.NET 2.0中,反向执行foreach的简单方法是什么? - In C# .NET 2.0, what's an easy way to do a foreach in reverse? 从C#中的代码访问$(SolutionDir)和$(DevEnvDir)等预构建宏的简单方法是什么? - What's an easy way to access prebuild macros such as $(SolutionDir) and $(DevEnvDir) from code in C#? 给定字典,查找包含特定字符集和字符串的所有可能单词的最佳方法是什么 - Given a dictionary, what's the optimal way to find all possible words that contains a particular set of characters and a string 在C#中从bool转换为字符串的最简单方法是什么? - What's the easiest way to convert from a bool to string in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM