0

I want to get the substring from a string when i ll give the part of the string ...

For example : I have the string "Casual Leave:12-Medical Leave :13-Annual Leave :03".

Partial code is here:

      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 //
      }

what i do?

2
  • Split with "-" and the split with ":"
    – Shoban
    Commented Feb 22, 2012 at 11:08
  • If the format is fixed then Split the string first on - and then the individual ones on :
    – V4Vendetta
    Commented Feb 22, 2012 at 11:09

8 Answers 8

2

Not sure exactly what you want here, but:

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];

You'll need more handling of conditions for when the string is not in the expected format etc., but this should start you off.

0
1

See the below code

        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];
            }
        }
0
1

For what I understand, you may want to use Split() function from string class. Something like this:

string str = "Casual Leave:12-Medical Leave :13-Annual Leave :03";
string[] splittedStrs = str.Split(':', '-');
0
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);
        }
    }

Output :

  • 12
  • 13
  • 03
1
  • +1 for being the only one who's using regex here. Just wondering about why. Its just /(:[0-9]+)/.
    – C4d
    Commented Sep 25, 2014 at 12:32
0

If you want it to make dynamic then you will have to first split the string using '-' character and then you will get the array of strings a[0] = "Casual Leave:12", a[1] = "Medical Leave :13" and a[2] ="Annual Leave :03". Then again split each of the value using ':' character. then you can find the leave value at index 1 for each leave.

Eg: b[0]="Casual Leave", b[1] = "12". Then you can match if lbl.text = b[0].tosting() then txt.text=b[1].tostring().

0

If your string is always gonna be represented in this form Casual Leave:12-Medical Leave:13-Annual Leave:03 then you should split it on -

you get this

  • Casual Leave:12
  • Medical Leave :13
  • Annual Leave :03

Now as you need pull up the value by again splitting it up on :

and you get for the first split value

  • Casual Leave
  • 12
0

Is it this you are looking for:

    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();
    }
0

In this case, if I understand you correctly, you have multiple delimiters in your string (i.e., ":", "-"). You can use the String.Split method to create an array of parts that you can then traverse (http://msdn.microsoft.com/en-us/library/ms228388%28v=vs.100%29.aspx). In your case:

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

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

resulting in:

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

If you would like finer-grained control, you could use String.Substring as so:

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();
        }
    }
}

Hope this helps you get what you're after!

Not the answer you're looking for? Browse other questions tagged or ask your own question.