Transforming a string using Regex C# [duplicate] - c#

This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 8 years ago.
i've been kicking my self for a while now with regex, and unable to find a proper solution. I've been trying to transform a string using Regex.Replace() method in C# which should prepend 0 to existing string if length is less than 5, the conversion might be as follow
Input String ----------- Output String
12345 ----------- 12345
123 ----------- 00123
123456 ----------- 123456
any help will be appreciated

You can use String.PadLeft: "1234".PadLeft(5, '0')

It can be like
var outputString = Regex.Replace(inputString, #"\d+", n => n.Value.PadLeft(5, '0'));
but you don't really need regex in this case.

You don't need any regex
if(str.Length < 5){
for(var i = 0; i < 5 - str.length; i++)
str = "0" + str;
}
The above is all you need. What you're doing is if the input string's Length is less than 5, you're just subtracting and looping, adding them.

Related

How to specify number of digits for a decimal type with .ToString() [duplicate]

This question already has answers here:
How to format floating point value with fix number of digits?
(3 answers)
How do I format a Decimal to a programatically controlled number of decimals in c#?
(5 answers)
Closed 3 years ago.
I would like to specify that I want 6 digits when I convert a value from a decimal to a string. I have the following code.
decimal bound = (decimal) field.MaxVal; //trait.MaxVal is a Nullable decimal...
// my debugger shows that bound contains the value 20.0000000000
int numDigits = 6;
string num = numDigits.ToString();
string output = bound.ToString("G" + num);
// output is '20' i expected '20.0000'
return output;
Any ideas on how I would do this.
I am focused on getting a total of 6 numbers in my string as opposed to 4 decimal places!
NOTE: bound will may have more or less than 2 digits in the whole number part.
string output = bound.ToString("F" + num);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?redirectedfrom=MSDN#FFormatString
F stands for fixed-point.

c sharp Programming , .net framework [duplicate]

This question already has answers here:
Formatting a C# string with identical spacing in between values
(7 answers)
Closed 3 years ago.
I have some code and string i want the string to be fixed length of 15 if
string length is less than 15 than i want to add spacing to make my string 15 character wide for printing purpose
i have tried
(string.format("{xxxxxxxxxxxxxxx}",myRow.Cells["PREMIER REG"].Value.ToString())
Input string was not in correct format exception thrown
myGraphic.DrawString("S# | DESCRIPTION | QTY | PRICE
| SUBTOTAL", myFont, Mybrash, xMargin, ySpacing);
ySpacing += 7;
myGraphic.DrawLine(myPen, xMargin, yMargin, 50, 0);
ySpacing += yMargin;
foreach (DataGridViewRow myRow in dataGridViewBill.Rows)
{
int i = 1;
myGraphic.DrawString(
i+ "" +
myRow.Cells["gvProductTitle"].Value.ToString() +
myRow.Cells["gvQuantity"].Value +
myRow.Cells["gvPrice"].Value +
myRow.Cells["gvTotal"].Value
, myFont, Mybrash, xMargin, ySpacing);
ySpacing += yMargin;
}
The problem in printing gvquantity value is being to pulled to product column
try this:
myRow.Cells["gvQuantity"].Value.ToString().PadRight(15);
String formatting allows you to specify the width (and whether to left or right align) by putting a comma after the format specifier and then the field width like:
string.format("{0,15}", myRow.Cells["PREMIER REG"].Value.ToString());
See
https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8#controlling-formatting

c# deleting the 0s of an int, but not if it is 20 [duplicate]

This question already has answers here:
How to remove leading zeros using C#
(9 answers)
Closed 5 years ago.
In my string start can be all sorts of numbers (1 until 99), but when the numbers get generated, 1 always get shown like 01. Is there an way to remove that 0, without if the number is 20 that the number get changed to 2?
You can use the String.TrimStart method:
string num = "0001";
num = num.TrimStart('0');
Another solution:
if (start.ElementAt(0) == '0')
start = start.Remove(0, 1);
or shorter:
start = start.ElementAt(0) == '0' ? start.Substring(1) : start;

Formatting Phone Number by adding zeros to the left [duplicate]

This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 6 years ago.
I have phone numbers like this :
452341
12
45789632
Is there a way i can format them like this way :
00452341
00000012
45789632
You cam use Padding:
Number.PadLeft(length, '0');
For example:
string num = "1234";
num.PadLeft(10, '0');
The result will be 0000001234
ToString("D8")
Here - MSDN - Decimal ("D") Format Specifier
NOTE:
PadLeft(length, '0'); does not work for negative numbers
Ex: (-1).Padleft(5, '0') --> 000-1
Use format with leading zeroes (Example for 8 symbols in number):
452341.ToString("D8");
if you have already strign use solution of Ashkan:
"452341".PadLeft(8, '0');

How to format a number such that first 6 digits and last 4 digits are not hidden

How to format a number such that first 6 digits and last 4 digits are not hidden
such that 111111111111111 looks like 111111****1111
You can also use LINQ, substituting chars with indexes more than 5 and less than number.Length - 4 with *:
string number = "111111111111111";
string res = new string(number.Select((c, index) => { return (index <= 5 || index >= number.Length - 4) ? c : '*'; }).ToArray());
Console.WriteLine(res); // 111111*****1111
One simple way to do this is to slit the input..
int number = 111111111111111;
string firstsix = number.ToString().Substring(0,6) //gets the first 6 digits
string middlefour = number.ToString().Substring(6,4) //gets the next 4
string rest = number.ToString().Substring(10, number.ToString().Lenght) //gets the rest
string combined = firstsix + "****" +rest;
You need to use \G anchor in-order to do a continuous string match.
string result = Regex.Replace(str, #"(?:^(.{6})|\G).(?=.{4})", "$1*");
DEMO
IDEONE

Categories

Resources