![]() |
|
[C#] Splitting text into strings - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Splitting text into strings (/showthread.php?tid=5669079) |
Splitting text into strings - Kαѕρer - Jul 13, 2017 If I have a .txt file with this for example: username1:password1 username2:password2 username3:password3 How am I able to load first username1 into the "username" string and password2 into the "password" string? And then once that's done by logging in, going back to the login screen and then loading username2 in the "username" string and password2 in the "password" string? string Accounts = File.ReadAllText("C:\\file.txt"); I tried this but then I end up getting all the accounts in that string and that's not how I want it. I know this sounds confusing, but I can't explain it better. I hope someone can help me out. Thanks! * - 0xDEAD10CC - Jul 13, 2017 File.ReadLines/ReadAllLines() and string.Split() function. It's not confusing at all and pretty simple to do. * - Kαѕρer - Jul 14, 2017 (Jul 13, 2017 06:48 PM)0xDEAD10CC Wrote: File.ReadLines/ReadAllLines() and string.Split() function. It's not confusing at all and pretty simple to do. Yeah, but how can I make from the part before the ":" a string and from the part after it another string? * - Abducted - Jul 14, 2017 Code List<string[2]> accounts = new List<string[2]>();* - 0xDEAD10CC - Jul 14, 2017 (Jul 14, 2017 03:27 AM)Kαѕρer Wrote: Yeah, but how can I make from the part before the ":" a string and from the part after it another string? If you really "knew" you would have been able to comprehend that I gave you the solution. I already answered this question... * - Siy - Jul 14, 2017 You find your way to get a string, line for line, then you use this: string username = line.Split(':')[0]; string password = line.Split(':')[1]; * - Oliver Sykes - Jul 14, 2017 protip: You won't just call .Split() once. It would be easy to spoonfeed code, but trust me, it makes it a pain in the ass to learn later on. * - temp19 - Jul 15, 2017 Go in Google.com and search how you can read a TXT FIle. Then search how does String.Split function works. And then figure out by your self . * - idb. - Jul 15, 2017 (Jul 14, 2017 06:00 PM)Siy Wrote: You find your way to get a string, line for line, then you use this: What if the password contains a :? OP, use SubString and IndexOf. * - BahNahNah - Jul 15, 2017 (Jul 15, 2017 02:46 AM)idb. Wrote: What if the password contains a :? This should work too. Code string[] split = line.Split(new char[] { ':' }, 2);* - 0xDEAD10CC - Jul 15, 2017 (Jul 15, 2017 02:46 AM)idb. Wrote: What if the password contains a :? Split still works as shown.. * - idb. - Jul 15, 2017 (Jul 15, 2017 03:59 PM)BahNahNah Wrote: This should work too. Mmmmmmmm. Forgot about that overload of String.Split. Good one bud! (Jul 15, 2017 05:52 PM)0xDEAD10CC Wrote: Split still works as shown.. And in the instance I quoted, under the circumstances I stated, would return an inaccurate password. Bah's solution is accurate. Other dudes was not. * - 0xDEAD10CC - Jul 17, 2017 (Jul 15, 2017 10:17 PM)idb. Wrote: Mmmmmmmm. Forgot about that overload of String.Split. Good one bud! In that context it seemed like you were suggesting to use the methods Substring() and IndexOf() over Split() entirely for the one reason you mentioned, and didn't suggest the alternative overload of IndexOf() at all. In that context it seemed like you were blaming the Split() function. People who mentioned the function could have been correct, it was just that one snippet that was not. And my analysis of your post appears to be correct too: Quote:Forgot about that overload of String.Split. Good one bud! I wasn't saying that his snippet with Split() was right, I was saying that Split() still works if used properly, which is still a true statement regardless of the context of your post. |