106 Replies
so if I type
hello
it would produce oellh
?no it just produces hello
it should produce this yes
right
so lets break it down
we need to be able to get the first and the last character, and "the middle"
then we just combine them
do you know how to get the first character?
did i not get it by doing word[0]
yep!
what about the last?
i used the for loop
that seems silly, doesnt it?
i tried to google it
we only need the last - why would we just a loop
would it be
word[word.length]
idk
yeah that works
there is a better way to do the same thing thou
word[^1]
the ^
means "start from the end"do i not need to convert it to an array
strings are already arrays
but when i do this
yeah you cant use that for assigning
you cant edit strings with indexing either
and remove the loop
you are trying to do everything in one go, which is just confusing
do it one step at a time
we know these already, so lets use them
now, how do we extract "the rest" of the string?
does var mean string or int?
or both
ive never used var
its a keyword that says "the type is obvious"
it tells the compiler to pick the type for you
screenshot from my IDE, where I have "type hints" enabled
ohhh okay
hm, are you using an old version of .NET?
like, maybe .net framework?
2022
thats visual studio
yes im using that
yes
visual studio is not the same thing as .NET
$newproject
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework.
.NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended.
https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
you have accidentally created a project using the old legacy runtime for .NET that was discontinued in 2016
so its been stale for 8 years
im using 6th one down
yeah, see how its marked "NO" in big red letters?
ive always used that one because it starts it off for me
you should be using .NET
not .NET Framework
(I know, very confusing names)
but the .NET doesn't give me the using system etc
or the name space
dont worry, thats just a template
there is a way to undo it
and get the "old" style program.cs back
shall i just open a .NET and copy and paste this into it
yeah that would work
okay ill do that now
see that box I marked with green? check it
and you will get the more familiar program.cs
yep!
my exam is on monday though
my coding one
oh this is for school? did your teacher specify a specific version to learn?
no im just practicing for it
ok
we have to moderate a code already given to us
so im practicing how to manipulate things
right
well, create the new project and copy your stuff over
okay ive done it
and the ^1 is wokring
so now i have the first and last characters
yep
now we need to get "the middle"
would that not use the for loop
and start from 1 instead of 0
you could
so i=1 instead
but there is again a better solution
okay im listening
if you index a string with a range, you get whats called a substring
word[x..y]
what values do you think we should use for x and y?you mean data type or variable name?
no I mean value
I dont suggest you actually write
x
and y
just type the "correct" valuesi dont understand
word[0..2]
for exampleerm
1..length-1
right!
and what is another way to type
length-1
?lastLetter-1
^1
^1-1
no
^1
wait so im taking out the last letter and the first letter to get the middle
yeah
okay
var middle = s[1..^1];
essentially, take everything from position 1 to the last, minus 1, since the upper bound is exclusiveokay
so the .. means
give me everything except what i stated
ok so we know the three parts
now we just need to combine them again
so i make a new variable
saying last + middle + first
kinda
but we're in a method right?
yes
so why not just return the "answer" directly?
ahh okay
wait but what would be the point in the method if im putting them back together in the main
you wouldn't
is this wrong
yes
look at what you return
ive changed it to word
words*
then its fine, but again, I would not create the
words
variable at all
I'd just return the answer
what does the $ do?
I included the "code golf" version here too, to show how small this code can be 🙂
thats string interpolation
its a better way of combining things to strings,
like...
$"Hello {name}! Welcome to our shop. You have {coins} gold coins to spend."
see how nice that reads?yes
okay so now it returns them all together
oh it works
which bit is not needed
this is what I did
oh okay yay
thank you
but yeah, you see how similar it is
you've helped me more than my teachers
sadly, that is often the case
well thank you anyway
can i add you so i can message you directly
fine if not
I don't help over DMs, just make a thread 🙂
okay :)
you can ping me inside that thread thou
if you want
okay will do
thank you again
this way, other people can read the thread and learn, or correct me if I say something wrong etc
its better for everyone 🙂
very true
if you are "done" here, type
/close
to mark the thread as answeredoki doki