C#正在拆分另一个字符串



我想拆分下一个字符串,它是"L",但由于某种原因无法工作。我已经成功地为我的第一个子字符串实现了这一点,它似乎是有效的,但这对我的第二个子字符串不起作用,它应该在控制台中以新行或第20个字符返回"L"。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace employeefinal
{
    class Program
    {
        static void Main(string[] args)
        {
            employee i = new employee();
            Console.WriteLine(i.getName());
            Console.ReadLine();
            Console.WriteLine(i.getCity());
            Console.ReadLine();

        }
        public class employee
        {
            string employeename = "Name:John How Smith L, U, 012, 2, 7, 2, 4";

            public employee()
            {
            }
            public string getName()
            {
                return employeename.Substring(0, 19).Trim();
            }
            public string getCity()
            {
                return employeename.Substring(19, 20).Trim();
            }
        }
    }
}

对于子字符串,第二个参数是子字符串的长度。如果你只想让getCity返回"L",你可以将其更改为:

    return employeename.Substring(20,1).Trim();

Substring()方法接受两件事,一是字符位置和该位置的长度。在您的代码中,GetName()从零位置返回到第19个位置,在第二个方法中,即GetCity()从第19位返回到该字符串中的其余字符。所以我想子串(19,2)会起作用。

相关内容

最新更新