Home > Net >  C # code exercises
C # code exercises

Time:10-19

Such as: the input string str1, delete the str1 which all numeric characters 0 to 9, output string after processing
Novice small white, solve, thank you

CodePudding user response:

With regular, this is my train of thought



 
Private static void Main (string [] args)
{
Console. WriteLine (" please input string... ");
String input=Console. ReadLine ();

Console. WriteLine ("=====separation line=====");

The string result=GetResult (input);
Console. WriteLine ($" result: {result} ");

Console.ReadKey();
}

The static string GetResult (string STR)
{
StringBuilder sb=new StringBuilder();

for (int i=0; I & lt; STR. Length; I++)
{
if (! The Regex. IsMatch (STR [I] the ToString (), "^ [0-9] $"))
{
Sb. Append (STR [I] the ToString ());
}
}
Return sb. ToString ();
}

CodePudding user response:

for (int i=0; I & lt; 10; I++)
{
STR=STR. Replace (i.T oString (), string. The Empty);
}

If it is a simple, direct the Replace, faster,
Regular good scalability
  •  Tags:  
  • C#
  • Related