如何从字符串值获取特定文本? [英] How to get specific text from string value?

查看:112
本文介绍了如何从字符串值获取特定文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串该票证已创建,编号供您参考使用TS894858"

在上面的字符串中,我想获取"TS894858"并显示一些位置.

能否以简单的方式帮助我.

我尝试过的事情:

我已经尝试过

I have string "The ticket has created with the number TS894858 use in your reference"

in the above string i want to get "TS894858" and show some where.

Could you please help me that how to get in simple way.

What I have tried:

I have tried

public static string getBetween(string strSource, string strStart, string strEnd)
{
    int Start, End;
    if (strSource.Contains(strStart) && strSource.Contains(strEnd))
    {
        Start = strSource.IndexOf(strStart, 0) + strStart.Length;
        End = strSource.IndexOf(strEnd, Start);
        return strSource.Substring(Start, End - Start);
    }
    else
    {
        return "";
    }
}



但这非常复杂.



But this is very complex.

推荐答案

不是那么复杂.使用String.Split将字符串分成一个数组,然后在数组中搜索一个以TS开头并包含数字的字符串.像这样的东西:
Not really that complex. Use String.Split to separate the string into an array, and then search through the array for one beginning with TS, and containing numbers. Something like:
String[] words = strText.Split(' ');
int value;
foreach (string word in words)
{
    if (word.StartsWith("TS") && int.TryParse(word.Substring(2), out value))
    {
        // looks like you got the value
        Console.WriteLine("found: {0}", word);
    }
}


简单的一个.
Simpler one.
public static string getBetween(string strSource, string strStart, string strEnd)
       {
         return  strSource.Replace(strStart, "").Replace(strEnd, "").Trim();
       }



带有验证:



with Validation:

public static string getBetween(string strSource, string strStart, string strEnd)
       {

           if (!string.IsNullOrWhiteSpace(strSource) && !string.IsNullOrWhiteSpace(strStart) && !string.IsNullOrWhiteSpace(strEnd))
               return strSource.Replace(strStart, "").Replace(strEnd, "").Trim();
           return "";
       }


在这种情况下,您可以使用正则表达式,但这取决于字符串的格式.
如果字符串始终包含模式"... number< ticket number> ....",则可以使用下面的代码.
In this case you might be able to use a regular expression, but it depends on the format of your string.
If the string always contains the pattern "... number <ticket number> ....", then you can use the code below.
// Add to the beginning of the file
using System.Text.RegularExpressions;

// The expression
// It basically say get any word containg letters and numbers 
// directly after the word "number" and a space
Regex regex = new Regex(@"number\s+(?<no>[\w\d]+)\s", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

string text = "The ticket has created with the number TS894858 use in your reference";
Match m = regex.Matches(text);
if (m.Success)
{
    string ticketNumber = m.Groups["no"].Value;
    // Do some stuff
}
else
{
    // No ticket number was found
}


还有其他可能性,但是如果没有更多样本数据,很难给出更好的解决方案.


There are other possibilities too, but without more sample data it is difficult to give a better solution.


这篇关于如何从字符串值获取特定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆