Help new to c# and need explanation with substring
Hey everyone I just started coding but now I'm learning about .IndexOf and substring I just don't know how it works. Like for example I wanted to do Initials (Take the first letter of the first name[text box 1]and the 2 first letters of the last name[text box 2]) as well as in 1 single textbox the initials same as before but it's not the same so can anyone please help me with this code 😭🙏🏿
Things I use
-Visual studio
-WindowsFormsApp
-c#
22 Replies
This is how my form looks like and what I wanna program. I want the initials to come in label3 and label5
String.Substring Method (System)
Retrieves a substring from this instance. This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
You want
.Substring(0, 1)
and .Substring(0, 2)
respectively
Or [0]
and [..2]
Angius
REPL Result: Success
Console Output
Compile: 607.561ms | Execution: 80.195ms | React with ❌ to remove this embed.
Try it with substring
Yes I know this but my problem is just what variable do I use and what about the indexOf because I need to use that one too
But anyways thank you for the help this far
You have no use for
IndexOf()
here
It exists to find a location of a specific characterAngius
REPL Result: Success
Result: int
Compile: 343.488ms | Execution: 22.406ms | React with ❌ to remove this embed.
Not useful when creating initials
What about it with the first and last name in one textbox can I not use lastindexOf to locate the last space and then use substring for those initials??
Sure
Or how would you suggest doing it
Well,
.Substring()
takes start index and length
As per the docs
So... the last name part would go from the index of the space
To the length of twoI'll try my best thank you for the help
@Angius
Does it work?
Also, the textbox text is already a string, no need to convert it
Well ye but it's not all capital letters (only the last letter) can I do somewhere do a ToUpper??
And if so where
You can use toupper on any string
So use it where needed
On that combined string for example
Uh?
So how do I do the result to upper then
(a+b).ToUpper()
?Omg thank you so much lmao now the other half
@Angius OMG IT WORKED FINALLY THANK YOU!!!!!!!
Oh and btw I just noticed your profile you do YouTube and I used to watch your videos (if it's you) very long but entertaining so ye keep up the amazing work dude
Don't forget to mark it as solved. 😃