Alpha2k
Alpha2k

Reputation: 2241

Retrieving a line of text splited in 2 or more Strings

I want to retrieve the info of a line of text from a textfile in 2 different strings...

This is the situation, getting the password line from pwd.txt :

String pwdRetrieved = retreivePwd.getPwd("pwd.txt"); 

However the password is crypted and it needs 2 valors, the password itself + a "key", like this:

4B5CB9348D5ADB733D43C2FB57A6A971-admin_pwd

admin_pwd is the "key" or "reference" to the encrypted password.

Now what I want to do is get 4B5CB9348D5ADB733D43C2FB57A6A971 into a string and admin_pwd into another string, is that possible?

More specific, i want to read from the .txt until the character "-" is found and store it into a String, then i want it to keep reading after "-" and store it into another string.

Upvotes: 0

Views: 91

Answers (6)

Sathesh
Sathesh

Reputation: 39

You can try this..

String[] pwd = pwdRetrieved.split("-");

After you can split this array value into individual strings.

Upvotes: 2

ADTC
ADTC

Reputation: 10121

You could do this:

String[] parts = pwdRetrieved.split("-");
String password = parts[0];
String key = parts[1];

Or do this:

int dashPosition = pwdRetrieved.indexOf("-");
String password = pwdRetrieved.substring(0, dashPosition);
String key = pwdRetrieved.substring(dashPosition + 1);

Upvotes: 1

Simulant
Simulant

Reputation: 20142

you could split your String at the - after reading the whole line like this:

String pwdRetrieved = retreivePwd.getPwd("pwd.txt"); 
String[] split = pwdRetrieved .split("-");
System.out.println(split[0]);
System.out.println(split[1]);

Upvotes: 1

Dima
Dima

Reputation: 8662

String[] split = pwdRetrieved.split("-");
String enc=split[0];
String pass=split[1];

Upvotes: 1

RobF
RobF

Reputation: 2818

Read the whole string, split on the "-" and retrieve the two parts from the created array:

String pwdRetrieved = retreivePwd.getPwd("pwd.txt"); 
String[] splitPwdRetrieved = pwdRetrieved.split("-");

String password = splitPwdRetrieved[0];
String key = splitPwdRetrieved[1];

Upvotes: 4

SimonPJ
SimonPJ

Reputation: 766

Using String's split method will do the trick:

String[] split = pwdRetrieved.split("-");

Will return a string array with the two strings you are after

Upvotes: 1

Related Questions