I
I
iXelper2018-08-17 15:08:03
C++ / C#
iXelper, 2018-08-17 15:08:03

Working with arrays (list) and characters -2-?

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

1 answer(s)
A
Alexey Mishchenko, 2018-08-17
@cup_alex

Let me explain in advance that the ParseToString method takes List as arguments, and returns a string in which exactly the elements of this array (list) are written. Regulars for your task can not be used.

class Program
  {
    public static string ParseToString(List<string> t)
    {
      string _return = "";
      
      for(int i=0;i<t.Count;i++)
      {
        if (i == t.Count-1)
        {
          _return += t[i];
          break;
        }
        _return += t[i] + " ";
      }
      return _return;
    }
    
    public static void Main(string[] args)
    {
      List<string> text = new List<string>(new String[]{"89192864554","+7926?874?22?44", "[email protected]" , "vk.com", "youtube"});
    
      List<string> phones = new List<string>(text.Where(x => x.StartsWith("8") || x.StartsWith("+7")).Select(x => x.Replace("?","")));
      List<string> sites = new List<string>(text.Where(x => x.Contains(".") && !x.Contains("@")));
      List<string> emails = new List<string>(text.Where(x => x.Contains(".") && x.Contains("@")));
    
      Console.WriteLine(ParseToString(phones));
      Console.WriteLine(ParseToString(sites));
      Console.WriteLine(ParseToString(emails));
      
      Console.ReadKey();
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question