Reputation: 358
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?
Upvotes: 0
Views: 1283
Reputation: 176886
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 :
Upvotes: 1
Reputation: 11
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!
Upvotes: 0
Reputation: 33857
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.
Upvotes: 2
Reputation: 11844
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];
}
}
Upvotes: 1
Reputation: 13335
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();
}
Upvotes: 0
Reputation: 38200
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
Now as you need pull up the value by again splitting it up on :
and you get for the first split value
Upvotes: 0
Reputation: 118
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().
Upvotes: 0
Reputation: 1852
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(':', '-');
Upvotes: 1