Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Xin hướng giải quyết, tình hình là sau khi tách ra đc 123, mình muốn gán biến "123" vào 1 biến int để tính toán trên hàm main, mong đc giúp đỡ

20:17 13-01-2023 273 lượt xem 3 bình luận

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        
        static void Main(string [] b)
        {


            demo();
           

            Console.ReadKey();
            
        }
        
        public static  void demo()
        { string b = "";
            Regex reg = new Regex(@"\d");
            string c = "";
            string[] A = new string[10];
            int i = 0;
            int d=0;
            foreach (Match test in reg.Matches("-ewfasd 123"))
            {
                b = test.ToString();
                A[i] = b;

                i++;


            }
            for (int j = 0; j < A.Length; j++)
            {
                
                c = String.Concat(c, A[j]);
                
            }
            d=int.Parse(c); 
            Console.WriteLine(c);
            

        }

    } }

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
mt1234 đã bình luận 20:40 13-01-2023

Bạn tự phát triển tiếp nhé:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string [] b)
        {
            int variable = demo();
            Console.WriteLine("Số lấy được là: " + variable);
            Console.ReadKey();
        }
        
        public static  int demo()
        {
            string b = "";
            Regex reg = new Regex(@"\d");
            string c = "";
            string[] A = new string[10];
            int i = 0;
            int d=0;
            foreach (Match test in reg.Matches("-ewfasd 123"))
            {
                b = test.ToString();
                A[i] = b;
                i++;
            }
            for (int j = 0; j < A.Length; j++)
            {
                c = String.Concat(c, A[j]);
            }
            d = int.Parse(c);
            return d;
        }
    }
}

 

Câu hỏi mới nhất