Agung Code
3 min readNov 19, 2020

Matching Part Number and Data Format in ASP NET

Problem
Creating an ASP NET project or using other frameworks at a reputable company, moreover, these companies produce various products in which they must use a part number as in Barcode with a format determined by the company. However, the problem is what kind of process can be done to match the part number data to that format.

Matching Part Number and Format ASP NET

Solution
If you have used char.IsDigit (), char.IsLetter, char.IsNumber or the regular expression (regex). These methods are very useful, therefore I will share how to use these methods and maybe later you can develop them.

Concept
Before I share the source code, I will explain the concepts that will be used. Suppose, we have a Part Number to match the predefined data format. From that Part Number, a loop will be made for all the Formats and then look for which format has the higher match based on the total number of characters that match.

Example

We have Part Number: 1234AB, 54AC78, 77458A, HH994, Format Data: NNSSNN, NNNNNS, SSNNSS, SNSNSN, SSNNN.

Note: N is numeric, S is String or Letter.

Question: According to the concept I mentioned earlier, we determine the Format for Part Number based on the total count character of part number according to the format

Project
Create a new project in the ASP NET Console Application with the name “PartNumberandFormat”.

Create 2 Field Array with the datatype string and fill in the 2 fields with Part Number and Format.

String[] PartNumber = {“1234AB”, “54AC78”, “77458A”, “HH994”}

String[] Format = {“NNSSNN”, “NNNNNS”, “SSNNSS”, “SNSNSN”, “SSNNN”}

Don’t forget to create the Result field to accommodate the most mathing format results and the PreviousCount and AfterCount integer fields.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace PartNumberdanFormat
{
class Program
{
static void Main(string[] args)
{
try {
Console.WriteLine("Author: AgungPanduan.com");
Console.WriteLine(DateTime.Now);
string[] PartNumber = { "1234AB", "54AC78", "77458A", "HH994" };
string[] Format = { "NNSSNN", "NNNNSS", "SSNNSS", "SNSNSN", "SSNNN" };
bool valid = true;
int PreviousCount = 0;
int AfterCount = 0;
string form = "";
string error = "";
List<string> Result = new List<string>();

foreach (string Part in PartNumber)
{
PreviousCount = 0;
error = "";
foreach (string formData in Format)
{
AfterCount = 0;
if (Part.Length != formData.Length)
{
valid = false;
}
else
{
valid = true;
if (Part != null && Part.Trim() != "")
{
for (int i = 0; i < Part.Length; i++)
{
if (formData.Substring(i, 1) == "N")
{
if (char.IsNumber(Part, i))
{
AfterCount++;
}
}
if (formData.Substring(i, 1) == "S")
{
if (char.IsLetter(Part, i))
{
AfterCount++;
}
}
}

}

}

if (PreviousCount < AfterCount && valid)
{
form = formData;
PreviousCount = AfterCount;

if (AfterCount < formData.Length) {
error = "But not quite right with total correct char is " + AfterCount;
}
else if (AfterCount == formData.Length) {
error = "";
}
valid = true;
}
}

Result.Add("Part Number: " + Part + " Using Format: " + form + " " + error);

}

foreach (string result in Result)
{
Console.WriteLine(result);
}
}
catch(Exception e) {
Console.WriteLine(e.Message);
}

}
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Agung Code
Agung Code

Written by Agung Code

0 Followers

Programmer, System Analyst, Blogger, Content Writer

No responses yet

Write a response