Actually, everything in the picture. Yuzver can enter three types of numbers: +79, 79, 89 with the same body. If the number starts at +79, then you need to remove three characters, if with 79 or 89, then two, and instead put +79.
How to implement?
Actually, everything in the picture. Yuzver can enter three types of numbers: +79, 79, 89 with the same body. If the number starts at +79, then you need to remove three characters, if with 79 or 89, then two, and instead put +79.
How to implement?
Good day!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static string StringRepl(string Str) { string pattern = @"^(\+79|79|89)[ ]?"; string Result; Regex reg = new Regex(pattern); Result = reg.Replace(Str,"+79"); return Result; } static void Main(string[] args) { while (1 == 1) { string StrA; StrA = Console.ReadLine(); Console.WriteLine(StringRepl(StrA)); if (Console.ReadKey().Key == ConsoleKey.E) { break; }; } } } } ^ . - MihailPwI propose to do better in the comments @ Andrew, otherwise:
string nomertelefona = GetValidString(inputstring); string GetValidString(string inputstring) { if (inputstring.StartWith("+")) inputstring = inputstring.Substring(1); return string.Format("+79{0}", inputstring.Substring(2)); } If you need the extension method:
public static string GetValidString(this string inputstring) // Метод расширения { if (inputstring.StartWith("+")) inputstring = inputstring.Substring(1); return string.Format("+79{0}", inputstring.Substring(2)); } Make a phone input by a mask, you can search for a ready control (for the benefit of a bunch of them) or do something like this yourself:
The selection of a country code can be made, for example, from a drop-down list.
Right, it’s as if you’ve never in your life filled out any form in applications or on websites.
TextBox has useful properties for organizing such input, such as MaxLength .
So that the user can enter only numbers, you can use, for example, this answer .
Well, do not forget to set these "pieces" the necessary values TabIndex
Upd. I haven't opened WinForms long time, it turns out that there is a standard out-of-box control - MaskedTextBox , which has the Mask property and even a set of standard masks to choose from.
Below are screenshots of what you can write from ComboBox + MaskedTextBox
Source: https://ru.stackoverflow.com/questions/708443/
All Articles
StartsWithandSubstringmethodsStartsWithSubstring. They will be enough. - Zufir