[英]String Get/Update Numeric Value After Specific Character
我有一個帶有字母數字輸入的控制台應用程序。 我想從字符的特定 position 中提取/更新數值。 所有角色都是獨一無二的。 輸入中沒有相同的字符。 例如,在我的程序中,我想從特定的字符 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;
}
}
}
這是您的方法的示例代碼。 在這里運行代碼
在最壞的情況下,時間復雜度將為 O(N),N -> 輸入的長度
static string GetNumericValue(string input, char c)
{
int charIndex = input.IndexOf(c);
StringBuilder sb = new StringBuilder();
int decimalCount = 0;
for (int i = charIndex + 1; i < input.Length; i++)
{
if (char.IsNumber(input[i]) || input[i] == '.')
{
if (input[i] == '.') decimalCount++;
if (decimalCount > 1) break;
sb.Append(input[i]);
}
else break;
}
return sb.ToString();
}
static string UpdateNumericValue(string input, char c, double newVal)
{
var numericValue = GetNumericValue(input, c);
return input.Replace(c + numericValue, c + newVal.ToString());
}
您可以使用正則表達式來提取所需的部分:
$@"{c}([-+]?[0-9]+\.?[0-9]*)"
將匹配字符c
並以組 0 或 1 符號捕獲,后跟 1 個或多個數字, 后跟 0 或 1 個點,后跟 0 或多個數字
using System.Text.RegularExpressions;
// returns an empty string if no match
static string GetNumericValue(string input, char c)
{
Regex regex = new Regex($@"{Regex.Escape(c.ToString())}([-+]?\d+\.?\d*)");
var match = regex.Match(input);
if (match.Success)
{
return match.Groups[1].Value;
}
return string.Empty;
}
替換可以使用上面的值,並將第一個匹配的字符串替換為預期的數字:
// returns the unchanged input if no match
static string UpdateNumericValue(string input, char c, double newVal)
{
var needle = $"{c}{GetNumericValue(input, c)}"; // get the numeric value prepended with the searched character
if (!string.IsNullOrEmpty(needle))
{
var regex = new Regex(Regex.Escape(needle));
return regex.Replace(input, $"{c}{newVal.ToString()}", 1); // replace first occurence
}
return input;
}
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();
input = "ABCDEF";
Console.WriteLine(GetNumericValue(input, 'C'));
//Expected output=""
Console.WriteLine(UpdateNumericValue(input, 'C', 42));
//Expected output="ABCDEF"
input = "ABCDEF";
Console.WriteLine(GetNumericValue(input, 'F'));
//Expected output=""
Console.WriteLine(UpdateNumericValue(input, 'F', 42));
//Expected output="ABCDEF"
input = "666ABC666ABC555";
Console.WriteLine(GetNumericValue(input, 'C'));
//Expected output="666"
Console.WriteLine(UpdateNumericValue(input, 'C', 42));
//Expected output="666ABC42ABC555"
我有一個更容易理解的解決方案:
static string GetNumericValue(string input, char c)
{
int indexStartOfValue = input.IndexOf(c) + 1;
int valueLength = 0;
while(Char.IsDigit(input[indexStartOfValue + valueLength]) || input[indexStartOfValue + valueLength] == '.')
valueLength ++;
return input.Substring(indexStartOfValue, valueLength);
}
static string UpdateNumericValue(string input, char c, double newVal)
{
var numericValue = GetNumericValue(input, c);
return input.Replace(c + numericValue, c + newVal.ToString());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.