C
C#3w ago
~Nost~

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#
No description
22 Replies
~Nost~
~Nost~OP3w ago
This is how my form looks like and what I wanna program. I want the initials to come in label3 and label5
Angius
Angius3w ago
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.
Angius
Angius3w ago
You want .Substring(0, 1) and .Substring(0, 2) respectively Or [0] and [..2]
MODiX
MODiX3w ago
Angius
REPL Result: Success
record Person(string First, string Last);
var people = new Person[]{
new("Bob", "Ross"),
new("Margaret", "Thatcher"),
new("Bill", "Gates"),
new("Harry", "Potter"),
new("Commandor", "Shepard"),
};

foreach (var person in people)
{
var fni = person.First[0];
var lni = person.Last[..2];
Console.WriteLine($"{person.First} {person.Last} — {fni}{lni}");
}
record Person(string First, string Last);
var people = new Person[]{
new("Bob", "Ross"),
new("Margaret", "Thatcher"),
new("Bill", "Gates"),
new("Harry", "Potter"),
new("Commandor", "Shepard"),
};

foreach (var person in people)
{
var fni = person.First[0];
var lni = person.Last[..2];
Console.WriteLine($"{person.First} {person.Last} — {fni}{lni}");
}
Console Output
Bob Ross — BRo
Margaret Thatcher — MTh
Bill Gates — BGa
Harry Potter — HPo
Commandor Shepard — CSh
Bob Ross — BRo
Margaret Thatcher — MTh
Bill Gates — BGa
Harry Potter — HPo
Commandor Shepard — CSh
Compile: 607.561ms | Execution: 80.195ms | React with ❌ to remove this embed.
Angius
Angius3w ago
Try it with substring
~Nost~
~Nost~OP3w ago
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
Angius
Angius3w ago
You have no use for IndexOf() here It exists to find a location of a specific character
MODiX
MODiX3w ago
Angius
REPL Result: Success
"aaaaaaabaaaaa".IndexOf('b')
"aaaaaaabaaaaa".IndexOf('b')
Result: int
7
7
Compile: 343.488ms | Execution: 22.406ms | React with ❌ to remove this embed.
Angius
Angius3w ago
Not useful when creating initials
~Nost~
~Nost~OP3w ago
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??
Angius
Angius3w ago
Sure
~Nost~
~Nost~OP3w ago
Or how would you suggest doing it
Angius
Angius3w ago
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 two
~Nost~
~Nost~OP3w ago
I'll try my best thank you for the help
~Nost~
~Nost~OP3w ago
@Angius
No description
Angius
Angius3w ago
Does it work? Also, the textbox text is already a string, no need to convert it
~Nost~
~Nost~OP3w ago
Well ye but it's not all capital letters (only the last letter) can I do somewhere do a ToUpper?? And if so where
Angius
Angius3w ago
You can use toupper on any string So use it where needed On that combined string for example
~Nost~
~Nost~OP3w ago
Uh? So how do I do the result to upper then
Angius
Angius3w ago
(a+b).ToUpper()?
~Nost~
~Nost~OP3w ago
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
felsokning
felsokning3w ago
Don't forget to mark it as solved. 😃
Want results from more Discord servers?
Add your server