如何比较字符串的每个单词并返回1和0之类的值? [英] How to compare each word of a string and return values like 1 and 0?

查看:89
本文介绍了如何比较字符串的每个单词并返回1和0之类的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试下面的代码,但每次总和值只显示零

so i tried below code but every time the sum value showing zero only

for (i = 0; Yname[i] != null; i++)
          {

              for (j = 0; Pname[j] != null; j++)
                  if (String.Equals(Yname[i], Pname[j], StringComparison.OrdinalIgnoreCase) == false)
                  {
                      Sum = Sum + 1;
                  }
          }

推荐答案

我不是我写的代码,它给了我正数:

I don''t I tried your code as written, and it gave me positive numbers:
string[] Yname = { "hello", "there", "Paul" , null};
string[] Pname = { "goodbye", "there", "paul" , null};
int Sum = 0;
int i, j;
for (i = 0; Yname[i] != null; i++)
    {

    for (j = 0; Pname[j] != null; j++)
        if (String.Equals(Yname[i], Pname[j], StringComparison.OrdinalIgnoreCase) == false)
            {
            Sum = Sum + 1;
            }
    }

运行此,Sum为7

唯一可以获得零的方法是,如果你的任何一个字符串的第一个元素数组为null,或者如果所有字符串都相同,则Pname的第一个元素为null。



你想要实现什么?不是让代码工作,但为什么要编写这段代码? (为什么你这样写呢?)





好的,我会再试一次。 >
我开发了一个爱情计量器应用程序。当用户点击按钮时,他将他和他的伴侣名称输入到文本框中,他得到的结果如爱情,婚姻等

按钮单击我想要比较每个单个字符。




啊!试试这个:

Run this, and Sum was 7
The only way you would get zero, is if the first element of either of your string arrays was null, or if all the strings were the same, of the first element of Pname was null.

What are you trying to achieve? Not "get the code working" but why are you writing this code? (And why are you writing it like that?)


"ok i''ll try again.
me developing a love meter application.user enters his and his partner name into textboxes when user clicks the button he get results like "love","marriage" etc
in button click i want to compare each of the single characters."


Ah! Try this:

string Yname = "hello";
string Pname = "goodbye";
int Sum = 0;
int i, j;
Yname = Yname.ToLower();
Pname = Pname.ToLower();
foreach (char y in Yname)
    {

    foreach (char p in Pname)
        {
        if (y != p)
            {
            Sum++;
            }
        }
    }

(前两行只供我测试 - 你需要你的文本框)

此代码做你的第一个代码,但使用字符。因此,对于我显示的两个字符串,它给出了32 - 因为它计算了Yname中每个字符的Pname中的每个字符。



如果你想要的是两个字符串中的字符数的计数(我怀疑你这样做)然后从反转条件!===然后你会得到3 - 'e''加上''o''两次。

(The first two lines are just for me to test - you will want your textboxes)
This code does what your first code did, but using characters. So with the two string I show, it gives 32 - because it counts each of the characters in Pname for each of the characters in Yname.

If what you want is a count of the number of characters that are in both strings (and I suspect you do) then reverse the if condition from "!=" to "==" and you will get 3 - the ''e'', plus the ''o'' twice.


试试这段代码

try this code
string[] Yname = { "hello", "there", "Paul" , null};
string[] Pname = { "goodbye", "there", "paul" , null};
int Sum = 0;
int i, j;
foreach (string name in Yname)
{
      if (Pname.Contains(name))
      {
          Sum = Sum + 1; 
      }
}





根据您的评论更新解决方案



updated solution as per ur comment

string[] Yname = txtUrNa.Text.ToString().Split(',');
string[] Pname = txtPrNa.Text.ToString().Split(',');
int Sum = 0;
foreach (string name in Yname)
{
     if (Pname.Contains(name))
     {
        Sum = Sum + 1;
     }
}


这篇关于如何比较字符串的每个单词并返回1和0之类的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆