C
C#•3y ago
cyn

need help with string formatting

hello, need help on formatting my string outputs
25 Replies
cyn
cynOP•3y ago
this is my current output:
cyn
cynOP•3y ago
this is the output i'm trying to achieve
cyn
cynOP•3y ago
cyn
cynOP•3y ago
not quite sure what i am doing wrong here though (line 2)
for (int i = 1; i < 13; i++)
Console.WriteLine("{0,-1} {1,8}", i, numUser * i);
for (int i = 1; i < 13; i++)
Console.WriteLine("{0,-1} {1,8}", i, numUser * i);
appreciate your help, thanks in advance!
cumslvt13
cumslvt13•3y ago
I think you should consider using PadLeft/PadRight string methods
cumslvt13
cumslvt13•3y ago
String.PadLeft Method (System)
Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.
cumslvt13
cumslvt13•3y ago
using System;

public class Program
{
public static void Main()
{
for(var i = 0; i < 12; i++){
var number = i.ToString().PadRight(2);
var square = (i * i).ToString().PadRight(3);
Console.WriteLine($"{number}\t{square}");
}
}
}
using System;

public class Program
{
public static void Main()
{
for(var i = 0; i < 12; i++){
var number = i.ToString().PadRight(2);
var square = (i * i).ToString().PadRight(3);
Console.WriteLine($"{number}\t{square}");
}
}
}
cumslvt13
cumslvt13•3y ago
the output will be:
cyn
cynOP•3y ago
o thanks alot!
cyn
cynOP•3y ago
may i know if it's possible with this however?
cumslvt13
cumslvt13•3y ago
2/3 are the longest number in column This does pad left and you need pad right
cyn
cynOP•3y ago
ah ic thanks a lot <:AA_PinkBunny_Heart:866885190390775868>
cumslvt13
cumslvt13•3y ago
also it's possible add - sign
cyn
cynOP•3y ago
may i know how to achieve it
cumslvt13
cumslvt13•3y ago
using System;

public class Program
{
public static void Main()
{
for(var i = 0; i < 12; i++){
Console.WriteLine($"{i,-2}\t{i*i,-3}");
}
}
}
using System;

public class Program
{
public static void Main()
{
for(var i = 0; i < 12; i++){
Console.WriteLine($"{i,-2}\t{i*i,-3}");
}
}
}
cyn
cynOP•3y ago
o i see thanks alot ❤️
cumslvt13
cumslvt13•3y ago
Can you give em a link to this arcticle?
cyn
cynOP•3y ago
yes give me a second
cyn
cynOP•3y ago
String.Format Method (System)
Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see the Get started with the String.Format method section for a quick overview. See the Remarks section for general documentation for the String.Format method.
cyn
cynOP•3y ago
thanks a lot for helping me!!
cumslvt13
cumslvt13•3y ago
Thanks Also
cumslvt13
cumslvt13•3y ago
String.Format Method (System)
Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see the Get started with the String.Format method section for a quick overview. See the Remarks section for general documentation for the String.Format method.
cumslvt13
cumslvt13•3y ago
Next part of the article says about - sign
cyn
cynOP•3y ago
i am vry vry dumb <a:AA_PinkBunny_Depressed:866885192127086633>
cumslvt13
cumslvt13•3y ago
be attentive 🙂

Did you find this page helpful?