question

GaniTPT avatar image
0 Votes"
GaniTPT asked DuaneArnold-0443 answered

How do we check contains if string with blank space in c#

I am having the below data table values.

DataTable Dt = new DataTable();
Dt.Columns.Add("COL1", typeof(string));
Dt.Columns.Add("COL2", typeof(string));
Dt.Columns.Add("COL3", typeof(string));
Dt.Rows.Add("101", "XZ", "101XZ");
Dt.Rows.Add("101", "XY", "XY101");
Dt.Rows.Add("101", "ZZ", "101 ZZ");

I want to check, COL1 and COL2 values will be exists in COL3.

In the COL3 i have some spaces within the values. (101 ZZ).

so, the intention is, I want to check, COL1 and COL2 values will be exists in COL3.

How do we check..?


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered GaniTPT commented

This will find any row where COL3 has a space and report the row index. This solution only satisfies what you wrote in the title.

 var results = Dt
     .AsEnumerable()
     .Select((row, index) => new {Row = row, Index = index})
     .Where(item => item.Row.Field<string>("COL3")
         .Contains(" "))
     .ToList();
 if (results.Count >0)
 {
     results.ForEach(x => Debug.WriteLine($"{x.Index}, '{x.Row.Field<string>("COL3")}'"));
 }


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

getting error.

86500-image.png


0 Votes 0 ·
image.png (25.9 KiB)

Seems odd, if count is greater than 0 there is at least one row and should be a valid column name. Perhaps try asserting the value e.g.


 if (results.Count > 0)
 {
     foreach (var x in results)
     {
         if (x.Row["COL3"] != DBNull.Value)
         {
             Debug.WriteLine($"{x.Index}, '{x.Row.Field<string>("COL3")}'");
         }
     }
 }
0 Votes 0 ·

Thanks s lot..

0 Votes 0 ·
DuaneArnold-0443 avatar image
2 Votes"
DuaneArnold-0443 answered GaniTPT commented

@GaniTPT


Basically, you detect if column3 data has a space in it with the string.contains().

https://docs.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-5.0

You then use the string.Replace() to replace " " (the space) with "" a null string value.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/trimming#:~:text=You%20can%20also%20remove%20a,all%20commas%20from%20a%20string.

if (column3.contains(" ") then
{
var stripspace = column3.replace(" ", "");

    // Then you do the compare using stripspace.

}
It should work for you, hopefully. :)

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

what about if i use the below data.

COL1 and COL2 should check in COL3.

If any values found in in COL3, it should return true, else false.

Note : Here COL3 value may be either "AM-120" or "AM 120"

         DataTable tblData = new DataTable();
         tblData.Columns.Add("COL1", typeof(string));
         tblData.Columns.Add("COL2", typeof(string));
         tblData.Columns.Add("COL3", typeof(string));
         tblData.Rows.Add("AM", "120", "AM-120");

How do we do this..?

0 Votes 0 ·
DuaneArnold-0443 avatar image
1 Vote"
DuaneArnold-0443 answered

if (colum3.contains("AM") && column3.contains("120") then
{
// then it's true
}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.