【发布时间】:2020-08-11 07:25:17
【问题描述】:
我有一个带有字母数字输入的控制台应用程序。我想从字符的特定位置提取/更新数值。所有角色都是独一无二的。输入中没有相同的字符。例如,在我的程序中,我想从特定的字符 R 中提取/更新数值。 R 是唯一的。它始终为 1,后跟数字。请帮忙。这是我的程序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp16
{
class Program
{
static void Main(string[] args)
{
var input = "JPR5AU75";
Console.WriteLine(GetNumericValue(input,'R'));
//Expected output=5
Console.WriteLine(UpdateNumericValue(input, 'R', 7));
//Expected output=JPR7AU75
input = "PR2.9AU75";
Console.WriteLine(GetNumericValue(input, 'R'));
//Expected output=2.9
Console.WriteLine(UpdateNumericValue(input, 'R', 3.5));
//Expected output=PR3.5AU75
input = "PKLR555AU75";
Console.WriteLine(GetNumericValue(input, 'R'));
//Expected output=555
Console.WriteLine(UpdateNumericValue(input, 'R', 765));
//Expected output=PKLR765AU75
Console.ReadLine();
}
static string GetNumericValue(string input, char c)
{
var value = "Get numeric value from position of charcter c";
return value;
}
static string UpdateNumericValue(string input, char c, double newVal)
{
var value = "Replace numeric value from input where character position starts with c";
return value;
}
}
}
【问题讨论】:
-
如果我们想将字符串
PR2.9AU75中的2.9替换为3.55,应该是PR3.5AU75还是PR3.55AU75? -
嗨@SowmyadharGourishetty,应该是3.55,因为新值是3.55。
-
我的逻辑也是这样,我的猜测是正确的。
-
@JobertEnamno - 您问题中的示例是唯一预期的结构吗?即
{prefix text}{prefix number}{postfix text}{postfix number}?