首页 新闻 会员 周边

C#字符串反向截取

0
悬赏园豆:5 [待解决问题]
string str = "84-1-2";
//想从2开始截取截取到2前面的-怎么做?
西瓜凉了半个夏的主页 西瓜凉了半个夏 | 初学一级 | 园豆:7
提问于:2016-04-15 13:39
< >
分享
所有回答(10)
0

先逆置,再根据“-”截取

小猴子爱吃桃 | 园豆:202 (菜鸟二级) | 2016-04-15 13:40
0
string[] aStr = str.split('-');

迴圈從aStr取出

RosonJ | 园豆:4910 (老鸟四级) | 2016-04-15 13:44
0

不管从前从后两个索引确定了就行了

小光 | 园豆:1766 (小虾三级) | 2016-04-15 13:47
0
 str = str.Substring(0, str.Length - 1);

 

大楚打码人 | 园豆:4313 (老鸟四级) | 2016-04-15 13:48
0

说说你到底想获取什么?

写代码的小2B | 园豆:4371 (老鸟四级) | 2016-04-15 14:15
0

使用LastIndexOf

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-15 15:08
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "84-1-2-4-5-3-6";

            string[] strarr = str.Split('-');

            for (int i = 0; i < strarr.Length/2; i++)
            {
                swap(ref strarr[i], ref strarr[strarr.Length-1 - i]);
            }
            string output = string.Empty;

            foreach(var item in strarr)
            {
                output += "-"+item;

            }
            Console.WriteLine(output.Substring(1));
            Console.ReadKey();
            
        }

        public  static void swap(ref string str1,ref string str2)
        {
            string temp = string.Empty;
            temp = str1;
            str1 = str2;
            str2 = temp;
        }
    }

    
}

KMSFan | 园豆:108 (初学一级) | 2016-04-15 16:28
0

我猜你是在做计算的词法分析部分吧?我最近也在做这个。去简单学一下正则表达式吧,用起来很方便的。

lulipro | 园豆:273 (菜鸟二级) | 2016-04-16 11:32
0

我这个好用:LastIndexOf(位置,个数)

无影飞絮剑 | 园豆:1155 (小虾三级) | 2016-04-16 13:38
0
string a = "84-1-2";
int num = a.LastIndexOf("-");
//结果 2
Console.WriteLine(a.Substring(a.LastIndexOf("-") + 1, a.Count()-num-1));
Console.Read();

 

广州大雄 | 园豆:188 (初学一级) | 2016-04-17 03:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册