从字符串中获取子字符串

时间:2012-02-22 11:05:18

标签: c# asp.net

我想在给出字符串的一部分时从字符串中获取子字符串...

例如:我有一个字符串“休假:12-医疗休假:13年假:03”。

部分代码在这里:

      Label label1 = new Label();
      label1.Text = (Label)item.FindControl(Label1); //label1.Text may be casual Leave or medical leave or others...
      if (label1.Text == substring(the given string ))
      {
          //suppose label1.Text ="Casual Leave" means i put 12 into the textbox
          TextBox textbox = new TextBox();
          textbox.Text= //corresponding casual leave value //
      }

我做什么?

8 个答案:

答案 0 :(得分:2)

不确定你想要什么,但是:

string start = "Casual Leave:12-Medical Leave :13-Annual Leave :03";

//Will give us three items first one being "Casual Leave:12"
string[] leaveItems = start.Split("-".ToCharArray());

//Will give us two items "Casual Leave" and "12"
string[] casualLeaveValues = leaveItems[0].Split(":".ToCharArray());

textBox.Text = casualLeaveValues[1];

当字符串不符合预期的格式等时,您需要更多处理条件,但这应该会让您失望。

答案 1 :(得分:1)

   const string input = "Casual Leave:12-Medical Leave :13-Annual Leave :03 ";      
   // Split on one or more non-digit characters.

   string[] numbers = Regex.Split(input, @"\D+");

   foreach (string value in numbers)
    {
        if (!string.IsNullOrEmpty(value))
        {
        int i = int.Parse(value);
        Console.WriteLine("Number: {0}", i);
        }
    }

输出

  • 12
  • 13
  • 03

答案 2 :(得分:1)

根据我的理解,您可能希望使用Split()课程中的string函数。像这样:

string str = "Casual Leave:12-Medical Leave :13-Annual Leave :03";
string[] splittedStrs = str.Split(':', '-');

答案 3 :(得分:1)

请参阅以下代码

        string text = "Casual Leave:12-Medical Leave :13-Annual Leave :03";
        string[] textarray = text.Split('-');
        string textvalue = "";
        foreach (string samtext in textarray)
        {
            if (samtext.StartsWith(<put the selected value from labe1 here>))
            {
                textvalue = samtext.Split(':')[1];
            }
        }

答案 4 :(得分:0)

如果你想让它变得动态那么你必须首先使用' - '字符分割字符串,然后你将获得字符串a [0] =“Casual Leave:12”,a [1] = “病假:13”和[2] =“年假:03”。然后再次使用':'字符拆分每个值。然后你可以在每个假期的索引1找到休假值。

例如:b [0] =“休假”,b [1] =“12”。那么你可以匹配if lbl.text = b [0] .tosting()然后txt.text = b [1] .tostring()。

答案 5 :(得分:0)

如果您的字符串始终以Casual Leave:12-Medical Leave:13-Annual Leave:03的形式显示,那么您应该将其拆分为-

你得到了这个

  • 休假:12
  • 病假:13
  • 年假:03

现在,您需要通过再次将其分解为:

来提取值

你获得第一个分割值

  • 休假
  • 12

答案 6 :(得分:0)

这是你在寻找:

    static void Main(string[] args)
    {
        const string text = "Casual Leave:12-Medical Leave :13-Annual Leave :03";

        foreach (var subStr in text.Split(":".ToCharArray()).Select(str => str.Trim()).SelectMany(trimmed => trimmed.Split("-".ToCharArray())))
            Console.WriteLine(subStr);

        Console.ReadLine();
    }

答案 7 :(得分:0)

在这种情况下,如果我理解正确,你的字符串中有多个分隔符(即“:”,“ - ”)。您可以使用String.Split方法创建一个可以遍历的部件数组(http://msdn.microsoft.com/en-us/library/ms228388%28v=vs.100%29.aspx)。在你的情况下:

 char[] delimiterChars = { ':', '-'};

 string text = @"Casual Leave:12-Medical Leave :13-Annual Leave :03";
 string[] words = text.Split(delimiterChars);

导致:

//words[0] = "Casual Leave"
//words[1] = "12"
//words[2] = "Medical Leave"
//words[3] = "13"
//words[4] = "Annual Leave"
//words[5] = "03"

如果您想要更精细的控制,可以使用String.Substring:

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

namespace StackOverflowDemoCode
{
    class Program
    {
        static void Main()
        {
            // Dictionary string
            const string s = @"Casual Leave:12-Medical Leave :13-Annual Leave :03";
            const int lengthOfValue = 2;

            // Convert to make sure we find the key regardless of case
            string sUpper = s.ToUpper();
            string key = @"Casual Leave:".ToUpper();

            // Location of the value
            // Start of the key plus its length will put us at the end
            int i = sUpper.IndexOf(key) + key.Length;

            // pull value from original string, not UPPERCASE string
            string d = s.Substring(i, lengthOfValue);
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}

希望这可以帮助你获得你想要的东西!