【问题标题】:Reduce dynamically some portion of string in c#在c#中动态减少字符串的某些部分
【发布时间】:2017-06-01 06:25:24
【问题描述】:

我需要一个 C# 程序来做这样的事情..

我的输入将是:APPLE/A0AB

我需要的输出是:A0AB

所以“/”和该符号之前的所有内容都应该减少。

提前致谢

【问题讨论】:

  • 你应该给自己找一个 hpc(高薪顾问)或向我们展示你的一些努力
  • 我会推荐正则表达式。但是你能给我们展示一些你的示例代码吗?
  • “我需要一个 C# 程序”在 StackOverflow 上并不是一个好的开始。这不是免费的编码服务。请冒险查看how-to-ask
  • 看看String类的methods,看看有没有什么可以用的...
  • 对不起,我的态度不好,但需要专家的帮助。

标签: c# winforms


【解决方案1】:

示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
  public class Program
{
 public static void Main(string[] args)
{
 string input=" APPLE/A0AB";
Console.WriteLine(input.Split('/')[1]);
}
}
}

输出: A0AB

在线运行: http://rextester.com/QJE14484

【讨论】:

  • 但是如果'/'的个数不知道会有问题。这种情况下我该怎么办。
  • @SiyamSarker 这取决于你想让程序做什么。
【解决方案2】:

你可以使用SubString方法。

input.Substring(input.LastIndexOf("/")+1)

你的代码应该是这样的:-

 string input = " APPLE/A0AB";
 string output = input.Substring(input.LastIndexOf("/") + 1);

【讨论】:

  • 我知道了,但假设你不知道字符串中有多少个“/”,那么我该怎么办。
  • @SiyamSarker 重新表述您的问题,将这条信息包含在您的问题描述中
  • @SiyamSarker 当你的字符串是APPLE/A0AB/PEAR/P0AP时你会怎么做,你会减少哪一部分?
  • 在最后一个“/”之前的所有字符串都将被删除。
  • @SiyamSarker 你至少可以通过接受他的回答为你编写代码来报答 Santosh 对你的帮助。如果你不知道这里是一个post that shows how to do it
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2023-03-03
相关资源
最近更新 更多