0

I have this string for example:

Username: tester1tt8e677 Password: b6a492e14c

I need to get out the username and the password only.

The password and user name are dynamically changing.

What is the best way doing it with Java, and how?

Thanks.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Eyal Sooliman
  • 1,876
  • 23
  • 29

7 Answers7

3

Do yourself a massive favour and get started on regular expressions. It will take more time than blindly copy-pasting an answer, but once you grasp the concept you can solve a huge number of problems with it.

You might want to check out this tutorial, which is java-specifiy and looks quite solid. Or just go ahead and google, you will find tons of information out there.

Once again - please do learn about regular expressions. You will not regret it.

panepeter
  • 3,224
  • 1
  • 29
  • 41
2

Try using a Matcher with a regex:

String pattern = "Username: (\\.+?) Password: (\\.+?)";

Matcher matcher = Pattern.compile( pattern ).matcher();
matcher.find();

You can then get your username and password from the first and second group:

String u = matcher.group(1);
String p = matcher.group(2);

However this does not sound like a good way to do whatever you are doing and you might want to consider another approach.

lars
  • 640
  • 4
  • 10
0

Rather than think of how you can work your solution around your problem, see if it can be broken up.

What do we want to do? We want to get values out of a String.

How? Rather than removing what we don't want, let's take out what we do want.

You then make the solution a lot simpler for yourself.

str = "Username: tester1tt8e677 Password: b6a492e14c";  
String[] splitStrings = str.split("\\s+");

System.out.println(splitStrings[1]);  //Username
System.out.println(splitStrings[3]);  //Password
Community
  • 1
  • 1
insidesin
  • 735
  • 2
  • 8
  • 26
0

You could use substring:

String String1 = "Username: testter1tt8e677";
System.out.println(String1.substring(0,10));

This should return "Username:" So:

System.out.println(String1.substring(10));

returns "testter1tt8e677".

JetStream
  • 809
  • 1
  • 8
  • 28
0

Try with below code:

 String s1 = "Username:tester1tt8e677 Password:b6a492e14c";


      // splitting String
      String[] splitString = s1.split(" ");


      for (String sp: splitString) {

    // Again Splitting
      String[] s2 = sp.split(":");
      System.out.println(s2[1]);
      }
0
String str="Username: tester1tt8e677 Password: b6a492e14c";
System.out.println(str.substring(0,str.indexOf(" ", str.indexOf(" ") + 1)).split(": ")[1]);
System.out.println(str.substring(str.indexOf(" ", str.indexOf(" ") + 1)+1,str.length()).split(": ")[1]);

Output:

tester1tt8e677
b6a492e14c
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
0

Well I have found my answer and it was pretty easy:

StringBuilder pass1 = new StringBuilder(pass).delete(0, 35); 
String resultString = pass1.toString();

StringBuilder user1 = new StringBuilder(user).delete(0, 10);
StringBuilder user2 = new StringBuilder(user1).delete(14, 35);
String resultString = user2.toString();

Thanks anyway (-:

Eyal Sooliman
  • 1,876
  • 23
  • 29