Search code examples
xamarin.android

Selecting specific lines in a multiline in array


i work with project in xamarin android :

1- i want to Selecting specific lines in a multiline array Where the length of the number > 11

2- and get Line when it contains or starts with the clip "ab"

for example randomly array line

145 12345678912752 iuy aber

1- length of the number > 11 = 12345678912752

2- starts with the clip "teq" =aber

my array code

           public void ReceiveDetections(Detections detections)
        {
            SparseArray items = detections.DetectedItems;
            if (items.Size() != 0)
            {
                txtView.Post(() => {
                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < items.Size(); ++i)
                    {
                        strBuilder.Append(((TextBlock)items.ValueAt(i)).Value);
                        strBuilder.Append("\n");
                    }
                    txtView.Text = strBuilder.ToString();

                });
            }
        }

Solution

  • 1- i want to Selecting specific lines in a multiline array Where the length of the number > 11

    2- and get Line when it contains or starts with the clip "ab"

    According to your description, you want to get string from array that meetting one of the two conditions above, am I right? If yes, I do one sample that you can take a look, I display the data that meetting your requirement in ListView.

     private ListView listview1;
        private string[] items;
        private List<string> resultarray=new List<string>();
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            items = new string[] {"145", "12345678912752", "iuy", "aber" };
            resultarray = getlines(items);
            listview1 = FindViewById<ListView>(Resource.Id.listView1);
            listview1.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, resultarray);           
        }
    
        private List<string> getlines(string[] items)
        {
           
           foreach(string item in items)
            {
               string resultString = Regex.Match(item, @"\d+").Value;
                if (resultString.Length>11 || item.Contains("ab"))
                {
                    resultarray.Add(item);
                }
               
            }
            return resultarray;
        }
    

    enter image description here