1. IRange接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gbxTest
{
    interface IRange
    {
        int endIndex
        {
            set;
            get;
        }
        int startIndex
        {
            set;
            get;
        }
    }
}

 

2. startBiggerThanEndException类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gbxTest
{
    class StartBiggerThanEndException : Exception
    {
        public StartBiggerThanEndException(string message)
            : base(message)
        { }
    }
}

 

3. TextEntry类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gbxTest
{
    class TextEntry : IRange
    {
        private int _start, _end;
        private string text;

        //对接口中属性的重写
        public int endIndex
        {
            set
            {
                this._end = value;
            }
            get
            {
                return this._end;
            }
        }
        public int startIndex
        {
            set
            {
                this._start = value;
            }
            get
            {
                return this._start;
            }
        }
        public void setIndex(int start, int end)
        {
            this.startIndex = start;
            this.endIndex = end;
        }
        //对字符串的赋值
        public string Text
        {
            set
            {
                text = value;
            }
            get
            {
                return this.text;
            }
        }
        //检查索引的正确性函数实现
        public bool checkIndex(int start,int end)
        {
            if (start < 0 || start > text.Length || end < 0 || end > text.Length)
            {
                throw new IndexOutOfRangeException("起始/结束索引超出字符串索引范围");
            }
            if (start > end)
            {
                throw new StartBiggerThanEndException("起始索引大于结束索引");
            }
            return true;
        }
        //获得字串
        public string selectSubString()
        {
            string subString = text.Substring(_start, _end);
            return subString;
        }

    }
}

 

4. getSubString_Click()方法

private void getSubString_Click(object sender, RoutedEventArgs e)
        {
            TextEntry test = new TextEntry();
            try
            {
                string tmpS = textString.Text.Trim();
                test.Text = tmpS;
                int tmpStart = int.Parse(startIndex.Text.Trim());
                int tmpEnd = int.Parse(endIndex.Text.Trim());
                bool flag = test.checkIndex(tmpStart, tmpEnd);
                if (flag)
                {
                    test.setIndex(tmpStart, tmpEnd);
                    subString.Text = test.selectSubString();
                }
            }
            catch (IndexOutOfRangeException e1)
            {
                subString.Text = e1.Message;
            }
            catch (StartBiggerThanEndException e2)
            {
                subString.Text = e2.Message;
            }
        }

 总代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace gbxTest
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void getSubString_Click(object sender, RoutedEventArgs e)
        {
            TextEntry test = new TextEntry();
            try
            {
                string tmpS = textString.Text.Trim();
                test.Text = tmpS;
                int tmpStart = int.Parse(startIndex.Text.Trim());
                int tmpEnd = int.Parse(endIndex.Text.Trim());
                bool flag = test.checkIndex(tmpStart, tmpEnd);
                if (flag)
                {
                    test.setIndex(tmpStart, tmpEnd);
                    subString.Text = test.selectSubString();
                }
            }
            catch (IndexOutOfRangeException e1)
            {
                subString.Text = e1.Message;
            }
            catch (StartBiggerThanEndException e2)
            {
                subString.Text = e2.Message;
            }
        }
       
    }
}