Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work on windows from app csharp visual studio 2015

I have windows form have datagridview name Grid1

this grid have 5 rows and column name Account No

what i need is to loop through five rows and add spaces from left and remove spaces from right

and in same time it must be have length 15 .

suppose i write account no 123321 what i need to do is loop through 5 records

then loop through column account no then make following

15 - length of account(123321)= 9

then add 9 spaces to left of number and not add any spaces to right

meaning add 9 times ""

and in same time if number have right space remove it and add it to left of number

but must length of number as 6 + spaces = 15

What I have tried:

Example

'         123321'
'        476890 '
first one as 123321 is correct result

second one as 576890 is wrong because it have right space 

all number with spaces must be 15

so please how to do that 
Posted
Updated 8-Jul-19 16:55pm
Comments
Patrice T 8-Jul-19 22:48pm    
166 questions so far and you still have not understood that we help you to fix your code ?
Did you thought to read the language documentation ?

1 solution

In C#, String.PadLeft does the same for you.
You can just trim the string and apply padding.
C#
string accountNo = "123321   ";
string result = accountNo.Trim().PadLeft(15, ' ');
------------------------------
output:
         123321

For more details, click here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900