简体   繁体   中英

String contains any char array

My problem is that i want to check if a string contains one or more of three character arrays i set up, and for the most part i got it to work but for some weird reason one line doesn't want to work and i don't know what I'm doing wrong.

The three arrays are

a = af lowercase letters

b = AF capital letters

c = 1-6 numbers

the program works for: lowercase, capital, numeric, lowercase + numeric, capital + numeric and lowercase + capital + numeric, but what isn't working is lowercase+capital which is (c.Any(x.Contains) == false && a.Any(x.Contains) == true && b.Any(x.Contains) == true)

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    Char[] a = { 'a', 'b', 'c', 'd', 'e', 'f' };
    Char[] b = { 'A', 'B', 'C', 'D', 'E', 'F' };
    Char[] c = { '1', '2', '3', '4', '5', '6' };



    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
        if(textBox2.Text != "")
        {
            Char[] x = textBox2.Text.ToCharArray();
            if (c.Any(x.Contains) == true && a.Any(x.Contains) ==false && b.Any(x.Contains)==false)
            {
                textBox1.Text = "num";
            }
            else if (b.Any(x.Contains) == true && c.Any(x.Contains) == false && a.Any(x.Contains) ==false)
            {
                textBox1.Text = "cap";
            }
            else if (a.Any(x.Contains) == true && c.Any(x.Contains) == false && c.Any(x.Contains) == false)
            {
                textBox1.Text = "low";
            }
            else if (c.Any(x.Contains) == false && a.Any(x.Contains) == true && b.Any(x.Contains) == true)
            {
                    textBox1.Text = "low&cap";
            }
            else if (a.Any(x.Contains) == true && c.Any(x.Contains) == true && b.Any(x.Contains) == false)
            {
                textBox1.Text = "low&num";
            }
            else if (b.Any(x.Contains) == true && c.Any(x.Contains) == true && a.Any(x.Contains) == false)
            {
                textBox1.Text = "cap&num";
            }
            else if (a.Any(x.Contains) == true && b.Any(x.Contains) == true && c.Any(x.Contains) == true)
            {
                textBox1.Text = "cap&num&low";
            }

        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}
}

Thanks in forward

EDIT

Never mind, i just figured it out The program found the lowercase only option first and didn't bother to look further...

Something like this?

Char[] lower = { 'a', 'b', 'c', 'd', 'e', 'f' };
Char[] upper = { 'A', 'B', 'C', 'D', 'E', 'F' };
Char[] number = { '1', '2', '3', '4', '5', '6' };

List<string> types = new List<string>();
if(lower.Any(l=>x.Contains(l))
    types.Add("low");
if(upper.Any(u=>x.Contains(u))
    types.Add("cap");
if(number.Any(n=>x.Contains(n))
    types.Add("num");

textbox1.Text = string.Join("&",types);

note that you don't have to convert the input string to a char array - string.Contains() will work as well.

Have you looked into writing an extension? They make it very nice!

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = textBox2.Text.MyContains();
}

Of course, you could change the name of that extension to something that works for you.

The extension would need to go into a static class, and could be as simple as this:

public static class Extensions {

  private static Char[] a = { 'a', 'b', 'c', 'd', 'e', 'f' };
  private static Char[] b = { 'A', 'B', 'C', 'D', 'E', 'F' };
  private static Char[] c = { '1', '2', '3', '4', '5', '6' };

  public static string MyContain(this string value) {
    Char[] x = value.ToCharArray();
    string result = null;
    if (a.Any(l => x.Contains(l))) {
      result = "low";
    }
    if (b.Any(c => x.Contains(c))) {
      result = String.IsNullOrEmpty(result) ? "cap" : result + "&cap";
    }
    if (c.Any(n => x.Contains(n))) {
      result = String.IsNullOrEmpty(result) ? "num" : result + "&num";
    }
    return result;
  }

}

The line that test for "low" is wrong, you test two time the array c
Change to

else if (c.Any(x.Contains) == false && a.Any(x.Contains) == true && b.Any(x.Contains) == true)

That error captures always a string with mixed case letter and the else if for "low&cap" is never reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM