Cấp bậc tác giả:

DOTNET

Sử dụng chức năng Left, Right và Mid

Được viết bởi webmaster ngày 08/04/2014 lúc 09:14 AM
Bạn cần lấy chuỗi bất kỳ trong xâu, nhưng không biết làm sao lấy, hướng dẫn dưới đây sẽ chỉ cho bạn cách thực hiện lấy chuỗi bất kỳ trong xâu từ trái qua phải, hoặc từ phải qua trái hoặc ở giữa.
  • 0
  • 7199

Sử dụng chức năng Left, Right và Mid

namespace LeftRightMid
{
    /// 
    /// Summary description for Class1.
    /// 
    class LeftRightMid
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            
            //assign a value to our string
            string myString = "This is a string";
            //get 4 characters starting from the left
            Console.WriteLine(Left(myString,4));
            //get 6 characters starting from the right
            Console.WriteLine(Right(myString,6));
            //get 4 characters starting at index 5 of the string
            Console.WriteLine(Mid(myString,5,4));
            //get the characters from index 5 up to the end of the string
            Console.WriteLine(Mid(myString,5));
            //display the result to the screen
            Console.ReadLine();
        }

        public static string Left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified lenght and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        } 
        public static string Right(string param, int length)
        {
            //start at the index based on the lenght of the sting minus
            //the specified lenght and assign it a variable
            string result = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param,int startIndex, int length)
        {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param,int startIndex)
        {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        }

    }
}

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML