Home > Software engineering >  How to find character after pipe "|" in C#
How to find character after pipe "|" in C#

Time:11-21

I would like to find character after | and check in a loop in C#.

Like I have Test|T1. After pipe the character could be anything like Test|T2 or Test|T3.

The first value is Table|Y and second value could be Test|T1, Test|T2 or Test|T3.

So I would like to check the character after | in else block.

  foreach (var testing in TestQuestion.Split(','))
  {
       if(testing.Equals("Table|Y"))
            {
        order.OrderProperties.Add(new OrderProperty("A")
        {
          ...
        });
        }

     else
          {
        //check the value after "|"
      }
                        
    

 }

So I would like to check the character after | in else block.

CodePudding user response:

like this

var s = "XXX|T1234";
var idx = s.IndexOf("|");
if (idx > -1) // found
{
     var nextChar = s.Substring(idx   1, 1);
}

if its possible that '|' is the last character then you need to check for that

CodePudding user response:

Another way to do this,

var tokens = test.Split('|');

if (tokens.FirstOrDefault() == "Test" && tokens.LastOrDefault() == "T1")
{

}

CodePudding user response:

You can do it this way (to test if value is T1 for example):

if (testing.Split('|')[0] == "Test" && testing.Split('|')[1] == "T1")
{
    
}
  •  Tags:  
  • c#
  • Related