本文介绍了获取子串 - 某些字符前的一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出拿到之前一切的最好方式 - 字符的字符串。一些示例字符串如下。前的字符串的长度 - 而不同,并且可以是任何长度

  223232-1.jpg
443-2.​​jpg
34443553-5.jpg

所以我需要,从0开始索引的向右前值 - 。因此,子会变成是223232,443,34443553


解决方案

 静态无效的主要(字串[] args)
    {
        字符串s =223232-1.jpg;
        Console.WriteLine(SEP(S));
        S =443-2.​​jpg;
        Console.WriteLine(SEP(S));
        S =34443553-5.jpg;
        Console.WriteLine(SEP(S));    Console.ReadKey();
    }    公共静态字符串九月(字符串s)
    {
        int类型l = s.IndexOf( - );
        如果(L&0)
        {
            返回s.Substring(0,1);
        }
        返回;    }

I'm trying to figure out the best way to get everything before the - character in a string. Some example strings are below. The length of the string before - varies and can be any length

223232-1.jpg
443-2.jpg
34443553-5.jpg

so I need the value that's from the start index of 0 to right before -. So the substrings would turn out to be 223232, 443, and 34443553

解决方案
    static void Main(string[] args)
    {
        string s = "223232-1.jpg";
        Console.WriteLine(sep(s));
        s = "443-2.jpg";
        Console.WriteLine(sep(s));
        s = "34443553-5.jpg";
        Console.WriteLine(sep(s));

    Console.ReadKey();
    }

    public static string sep(string s)
    {
        int l = s.IndexOf("-");
        if (l >0)
        {
            return s.Substring(0, l);
        }
        return "";

    }

这篇关于获取子串 - 某些字符前的一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:00