如何在字符串中附加任何数值 [英] How do I append any numeric values in a string

查看:67
本文介绍了如何在字符串中附加任何数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

考虑下面提到的这个字符串。我将从设置中获取要附加的零的数量。例如,这设置为4



正常字符串追加后

S1 - > S0001



C#或java脚本中是否有任何数字字符



我有什么尝试过:



我尝试使用split函数,其中我找到了 s 的值,如果下一个char是基于数字的数字长度即执行填充零。

解决方案

这有点复杂:您需要先找到数字,将其转换为数值,然后重建string:

  string  input =   S1; 
匹配m = Regex.Match(输入, @ ^(?< Prefix>。+?) (小于?号> \d +)

);
if (m.Success)
{
int number = int .Parse(m.Groups [ Number].Value);
string output = string .Format( {0} {1:0000},m.Groups [ 前缀]。值,数字);
...
}


Consider This string as mentioned below. I shall get the number of the zeros to be appended from a setting. for example this is set to 4

Normal string After appending
S1 --> S0001

Is there any numeric to chars in C# or java script

What I have tried:

I tried using split function where i find the value of s and if the next char is numeric based on numeric length ie performing a padding zeros.

解决方案

It's a little complex: you need to start by locating the number, convertit it to a numeric value, and then rebuild the string:

string input = "S1";
Match m = Regex.Match(input, @"^(?<Prefix>.+?)(?<Number>\d+)


"); if (m.Success) { int number = int.Parse(m.Groups["Number"].Value); string output = string.Format("{0}{1:0000}", m.Groups["Prefix"].Value, number); ... }


这篇关于如何在字符串中附加任何数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆