Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:19:21+00:00 2026-05-20T18:19:21+00:00

lets say i have 5 lines within a txt file called users.txt each line

  • 0

lets say i have 5 lines within a txt file called users.txt each line has the following information

username:password

how would i go about spliting each line within a txt file and store the username as one string and password as the other.

I have the code to grab a random line using this code. This code is used for another part of my project aswell so I dont want the code to be altered. I was thinking after the line has been grabbed call another function but I have no idea on how to split it with the :

private static string GetRandomLine(string file)
    {
        List<string> lines = new List<string>();

        Random rnd = new Random();

        int i = 0;


        try
        {
            if (File.Exists(file))
            {
                //StreamReader to read our file
                StreamReader reader = new StreamReader(file);

                //Now we loop through each line of our text file
                //adding each line to our list
                while (!(reader.Peek() == -1))
                    lines.Add(reader.ReadLine());

                //Now we need a random number
                i = rnd.Next(lines.Count);

                //Close our StreamReader
                reader.Close();

                //Dispose of the instance
                reader.Dispose();

                //Now write out the random line to the TextBox
                return lines[i].Trim();

            }
            else
            {
                //file doesn't exist so return nothing
                return string.Empty;
            }
        }
        catch (IOException ex)
        {
            MessageBox.Show("Error: " + ex.Message);
            return string.Empty;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:19:21+00:00Added an answer on May 20, 2026 at 6:19 pm

    This is much cleaner, and handles cases where the password might contain ‘:’s

    Of course I would expect you to ensure that passwords are not plain text and hashed password’s don’t contain any ‘:’s; But just in case they do, this is what would work:

    Split() will cause other problems.

            bool GetUsernamePassword(string line, ref string uname, ref string pwd)
            {
                int idx = line.IndexOf(':') ;
                if (idx == -1)
                    return false;
                uname = line.Substring(0, idx);
                pwd = line.Substring(idx + 1);
                return true;
            }
    

    .

                string username_password = "username:password";
                string uname = String.Empty;
                string pwd = String.Empty;
                if (!GetUsernamePassword(username_password, ref uname, ref pwd))
                {
                    // Handle error: incorrect format
                }
                Console.WriteLine("{0} {1} {2}", username_password, uname, pwd);
    

    btw. having said the above this won’t work (like all other solutions before this one) if the username has ‘:’ 😛 But this will handle the case where password has ‘:’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a file called foo.txt encoded in utf8: aoeu qjkx ñpyf
Let's say I have an NSSecureTextField in my app. Is it okay for me
I have an in-memory cache which stores a set of information by a certain
Let's say that I have 5 apps that I want to add ELMAH logging
I have come across a problem that I cannot see to solve. I have
I'm new to Clojure and have been using Compojure to write a basic web
hello i got a problem with reading from a file, i am trying to
Example: We have a conditional field. This conditional field is a radio button with
I have postgres database running on Amazon EC2 instance. I have few tablespaces created
we have created a SSRS report with a big chart that have multiple entries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.