I
I
iXelper2018-08-16 17:29:27
C++ / C#
iXelper, 2018-08-16 17:29:27

Working with arrays (list) and symbols?

There is an array:

List<string> text = ["89192864554","+7926?874?22?44", "[email protected]" , "vk.com", "youtube"];

(sometimes the elements of the array are filled in different ways)
Task: how to distribute the array so that the result would be like this:
string phone = "+7920?855?43?55  89192864554";
string site = "vk.com";
string email = "[email protected]";

in general, it is necessary to separate phones and websites into different variables))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
LiptonOlolo, 2018-08-16
@iXelper

List<string> text = ["89192864554","+7926?874?22?44", "[email protected]" , "vk.com", "youtube"];
List<string> phones = new List<string>(text.Where(x => x.StartWith("+") || x.StartWith("8")));
List<string> sites = new List<string>(text.Where(x => x.Contains(".") && !x.Contains("@")));
List<string> email = new List<string>(text.Where(x => x.Contins("@")));

As an option

J
Jan, 2018-08-16
@on1k

The most banal option is to use regular expressions and pass through the array.

A
Alexey Mishchenko, 2018-08-17
@cup_alex

List<string> text = new List<string>(new string[]{"+7920?855?43?55","+7926?874?22?44", "the-appbox.ru" , "vk.com", "+7898?55?6466"});
    
      List<string> phones = new List<string>(text.Where(x => x.StartsWith("+")).Select(x => x.Replace("?","")));
      List<string> sites = new List<string>(text.Where(x=>x.Contains(".")));
    
      string phones_str = "";
      string sites_str = "";
      
      for (int i=0;i<phones.Count;i++) {
        if (i == phones.Count-1)
        {
          phones_str += phones[i];
          break;
        }
        phones_str += phones[i] + " ";
      }
      
      for (int i=0;i<sites.Count;i++) {
        if (i == sites.Count-1)
        {
          sites_str += sites[i];
          break;
        }
        sites_str += sites[i] + " ";
      }
      
      Console.WriteLine(phones_str);
      Console.WriteLine(sites_str);
      Console.ReadKey();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question