C
C#14mo ago
Yuvi

❔ Output not as expected. Very beginner, learning ABC of programming.

https://paste.mod.gg/fymahzukuiyz/0 That's the code, It used have shown the output like (111) 222-3334 but its straight up 1112223334, anyone know what I am doing wrong? I might go to sleep so chances that I will reply tomorrow.
BlazeBin - fymahzukuiyz
A tool for sharing your source code with the world!
No description
34 Replies
MODiX
MODiX14mo ago
Pobiega
REPL Result: Success
Console.WriteLine($"{51231235823:(###) ###-####}");
Console.WriteLine($"{51231235823:(###) ###-####}");
Console Output
(5123) 123-5823
(5123) 123-5823
Compile: 448.243ms | Execution: 38.862ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
works fine oh, you treat your number as a string normally, thats a good idea but it doesnt work well with number formatting because its not a number
Yuvi
YuviOP14mo ago
Oh I see I see, thanks
Pobiega
Pobiega14mo ago
phone numbers should never be stored as numbers btw but for the purpose of formatting, its an alternative I'd probably prefer string slicing thou
Yuvi
YuviOP14mo ago
What's string slicing?
Pobiega
Pobiega14mo ago
one sec
Yuvi
YuviOP14mo ago
Also...I am not sure how to format integers since .format doesn't seem to work with integers like the way I learned it did with strings
Pobiega
Pobiega14mo ago
look at my example :p
Yuvi
YuviOP14mo ago
forgot what it was called, function or something, my programming vocabulary is a bit weak rn, I understand stuff in my mind but not know the name. Might need a dictionary xD
Pobiega
Pobiega14mo ago
im not using string.format
MODiX
MODiX14mo ago
Pobiega
REPL Result: Success
var phone = "12345678900";

Console.WriteLine(phone[4..7]);
var phone = "12345678900";

Console.WriteLine(phone[4..7]);
Console Output
567
567
Compile: 445.671ms | Execution: 85.733ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
thats string slicing
Yuvi
YuviOP14mo ago
Can you please breakdown how this works logically? My weak memory only allows me to learn that way. What does the $ sign represent specifically since that's the confusing element there.
Pobiega
Pobiega14mo ago
string interpolation
MODiX
MODiX14mo ago
Pobiega
REPL Result: Success
var name = "Steve";
Console.WriteLine($"Hello {name}!");
Console.WriteLine("Hello {name}!");
var name = "Steve";
Console.WriteLine($"Hello {name}!");
Console.WriteLine("Hello {name}!");
Console Output
Hello Steve!
Hello {name}!
Hello Steve!
Hello {name}!
Compile: 589.156ms | Execution: 84.215ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
see the difference with and without $?
Yuvi
YuviOP14mo ago
Ohhh right, seems like easier way to use {0} in a string
Pobiega
Pobiega14mo ago
yeah, it is it supports mostly the same format options too I wonder if it handles string slicing
MODiX
MODiX14mo ago
Pobiega
REPL Result: Success
Console.WriteLine($"{"51231235823":(###) ###-####}")
Console.WriteLine($"{"51231235823":(###) ###-####}")
Console Output
51231235823
51231235823
Compile: 554.292ms | Execution: 87.495ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
nope
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
it doesnt like the fact that its a string. so you'll need to slice it up
Yuvi
YuviOP14mo ago
Let me try slicing it and see if I can get it right that way
Yuvi
YuviOP14mo ago
I tried this, know what the error is or if I used in a way it wasn't meant to be used?
No description
Yuvi
YuviOP14mo ago
Actually maybe like this
No description
Yuvi
YuviOP14mo ago
Hmm but wasn't that like string formatting actually?
Pobiega
Pobiega14mo ago
you cant "Multislice" the string like htat
MODiX
MODiX14mo ago
Pobiega
REPL Result: Success
var s = "asd123hello";
Console.WriteLine($"({s[3..6]}) {s[6..]}");
var s = "asd123hello";
Console.WriteLine($"({s[3..6]}) {s[6..]}");
Console Output
(123) hello
(123) hello
Compile: 575.709ms | Execution: 84.472ms | React with ❌ to remove this embed.
Yuvi
YuviOP14mo ago
@Pobiega this works, thanks.
No description
Yuvi
YuviOP14mo ago
Also you know why did string formatting with numbers for this guy but not in my case?
No description
No description
Pobiega
Pobiega14mo ago
he is using a number, not a string just like I did in my example
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
thats an integer
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server