Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Friends,

I have made an app which will get username and password from a text file in the following form

username:password

These details then will be added to a combobox.

I want to make that text (username) before ":" should go in username field of the login form and text (password) after ":" should go in password field.

Please help how to do it?
Posted
Comments
[no name] 9-Jul-14 14:09pm    
Split()
zubair30 9-Jul-14 14:11pm    
please explain I complete new
Thanks7872 9-Jul-14 14:21pm    
if you are not able to understand it,then i don't think you will be able to read from text file. Further, instead of relying solely on others, if someone suggested something try learning about it. No one will be going to teach you here.
Bernhard Hiller 10-Jul-14 2:40am    
And what about a ":" in the password?
George Jonsson 10-Jul-14 4:43am    
My solution takes care of that.

That really is a poor idea, but...
C#
string input = "username:password";
string[] parts = input.Split(':');
if (parts.Length >= 2)
    {
    Console.WriteLine("Username : {0}", parts[0]);
    Console.WriteLine("Password : {0}", parts[1]);
    }


It's very, very insecure - so don't use any of your real passwords for testing (or real world data, if you are going to do that with it)
If anyone gets their hands on the text file, they could have your entire password list...
 
Share this answer
 
Comments
zubair30 11-Jul-14 20:13pm    
I have converted your C# code into VB code and used. Thanks, It worked.
OriginalGriff 12-Jul-14 2:19am    
You're welcome.
There are two ways of doing it
1) Using the split function as shown in solution 2 above
2) using the left right inbuilt functions as shown below (THIS IS VB6 Sample code):

VB
Dim str As String
Dim uname As String
Dim pwd As String

str = ":usernamepassword"
If InStr(1, str, ":") > 0 Then
    uname = Left(str, InStr(1, str, ":") - 1)
    pwd = Right(str, Len(str) - InStr(1, str, ":"))
Else
    uname = str
    pwd = ""
End If
 
Share this answer
 
You can also use regular expression with group names.

C#
// This expression allows the password part to be empty.
// Change the * in (?<pwd>.*) to a +, to require at least one character
     
System.Text.RegularExpressions.Regex expr = 
   new System.Text.RegularExpressions.Regex(@"^(?<user>[\S\s]+?)\s*:\s*(?<pwd>.*)$");

string userName = "";
string password = "";
System.Text.RegularExpressions.Match m = expr.Match("Donald Duck : Mickey!234");
if (m.Success)
{
    userName = m.Groups["user"].Value;
    password = m.Groups["pwd"].Value;   // No need to check if there is a password
}
 
Share this answer
 
v3
Comments
Bernhard Hiller 10-Jul-14 9:07am    
And what if the username contains a ":"?
George Jonsson 10-Jul-14 10:03am    
It works for that too.
You can try yourself if you have the time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900