简体   繁体   中英

If ASCII String Contains Letter or Number, Then Return Text

Still working on my project, and would like some more help. Here's my problem: I have a string of chars in CompanyID that may be full of " " spaces (derived from ASCII hex text). Some code I've tried won't detect the spaces (possibly because there's many hex characters that return spaces with different values), and the code (FIG B.) returns a value of all spaces. What I would like to do is search the string 'CompanyID' for any Letter/Number. If true, I need textBox10.Text = CompanyID; If not, I need to have output to a textbox as shown in Fig, A.

FIG. A:

else textBox10.Text = "No Value!";
if (val.Contains("No Value!")) textBox10.ForeColor = Color.Orange;

FIG. B:

// COMPANY ID
{
BinaryReader br3 = new BinaryReader(File.OpenRead(OpenFileDialog1.FileName));
                br3.BaseStream.Position = 0x110;
                Char[] charArray = br3.ReadChars(16);
                string CompanyID = new string(charArray);
                textBox10.ForeColor = Color.Black;
                textBox10.Text = CompanyID;
                br3.Close();
                {
                    // CODE REPLACEMENT
                    string val = CompanyID;
                    textBox10.ForeColor = Color.Black;
                    foreach (char c in CompanyID)
                    {
                        if (Char.IsDigit(c)) textBox10.Text = CompanyID;
                        else textBox10.Text = "No Value!";
                        if (val.Contains("No Value!")) textBox10.ForeColor = Color.Orange;
                    }
                }

Any help would be appreciated. Thank you for your time!

I used the following code to solve my problem. Someone suggested using IsWhiteSapce to detect if there was anything in a string (even tho the post got somehow deleted), and its been working for me. Thanks for the peeps who replied.

// DESCRIPTION
            {
                BinaryReader br7 = new BinaryReader(File.OpenRead(OpenFileDialog1.FileName));
                br7.BaseStream.Position = 0x1c8;
                Char[] charArray = br7.ReadChars(40);
                string Desc = new string(charArray);
                textBox11.Text = Desc;
                if (textBox11.Text.All(c => char.IsWhiteSpace(c)))
                {
                    textBox11.ForeColor = Color.Orange;
                    textBox11.Text = "No Value!";
                }
                else textBox11.ForeColor = Color.Black;

                br7.Close();

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