[英]Get substring from a string
當我將給出字符串的一部分時,我想從字符串中獲取子字符串...
例如:我有字符串“休閑假: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 //
}
我所做的?
不確定您要在這里做什么,但是:
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];
您需要對字符串不是預期格式等的條件進行更多處理,但這應該使您開始工作。
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);
}
}
輸出:
據我了解,您可能想使用string
類中的Split()
函數。 像這樣:
string str = "Casual Leave:12-Medical Leave :13-Annual Leave :03";
string[] splittedStrs = str.Split(':', '-');
見下面的代碼
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];
}
}
如果要使其動態,則必須首先使用'-'字符分割字符串,然后獲取字符串數組a [0] =“休閑假:12”,a [1] =“醫療假:13“和a [2] =”年假:03“。 然后再次使用':'字符分割每個值。 那么您可以在每個休假的索引1處找到休假值。
例如:b [0] =“休閑假”,b [1] =“ 12”。 然后,如果lbl.text = b [0] .tosting()然后txt.text = b [1] .tostring(),則可以匹配。
如果您的字符串始終以這種形式表示, Casual Leave:12-Medical Leave:13-Annual Leave:03
,則應將其拆分為-
你得到這個
現在,您需要通過再次將其分割為以下來拉出該值:
然后得到第一個分割值
您要的是這個嗎?
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();
}
在這種情況下,如果我對您的理解正確,那么您的字符串中就有多個定界符(即“:”,“-”)。 您可以使用String.Split方法創建一個零件數組,然后可以遍歷這些零件(http://msdn.microsoft.com/zh-cn/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();
}
}
}
希望這可以幫助您獲得所需的服務!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.