获取子串 - 某些字符之前和之后的所有内容

时间:2017-04-17 02:05:17

标签: c# asp.net

我正在尝试找到从输出字符串中获取每个变量值的最佳方法,如下所示:

SUCCESS Post Policy Success INVOICE No. :: WS1704003404 || Policy No :: 
59203313 || App No. :: 123456724 

这是我们得到的字符串输出,我们希望得到

的值
WS1704003404
59203313
123456724

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式,例如:

class Program
{
    static void Main(string[] args)
    {
        var input = " SUCCESS Post Policy Success INVOICE No. :: WS1704003404 || Policy No :: 59203313 || App No. :: 123456724 ";

        var pattern = @"::\s*(\w+)\s*";

        var matches = Regex.Matches(input, pattern);

        foreach (Match match in matches)
            Console.WriteLine(match.Groups[1].Value);

    }
}

答案 1 :(得分:0)

您可以使用$post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'timeline', 'government'); $connection_types = get_all_connection_types(); $direction_array = array(); for($i=0;$i<count($connection_types);$i++) { $direction_array[$i] = 'from'; // if you want then you can send the from as well; } $connected = new WP_Query( array( 'connected_type' => $connection_types, 'post_type' => $post_types, 'connected_items' => 'any', 'connected_direction' => $direction_array, 'posts_per_page' => 20, 'orderby' => 'meta_value', 'connected_orderby' => 'date', 'connected_order' => 'desc' ) ); Split功能获取所需的结果。首先用“||”拆分该字符串作为分隔符,然后对该字符串进行子串,并且只需要接受“::”

之后的任何内容
Substring

结果:

string Input = "SUCCESS Post Policy Success INVOICE No. :: WS1704003404 || Policy No :: 59203313 || App No. :: 123456724 ";
var result = Input.Split(new[] { "||" }, StringSplitOptions.None)
            .Select(x => x.Substring(x.IndexOf("::") + 3));