1

I would like to cut my string "ftp://user:password@ftp.domain.com:21" to have each article in a string

in other words, I want to have:

String ftp = "ftp.domain.com"

String user = "user"

String password = "password"

String port = "21"

This code that works but it's a little dirty, right?

String fullFtpAdress = "ftp://user:password@ftp.domain.com:21";
fullFtpAdress = fullFtpAdress.substring(6);
String[] parts1 = fullFtpAdress.split(":",2);
String user = parts1[0];
String[] parts2 = parts1[1].split("@");
String password = parts2[0];
String[] parts3 = parts2[1].split(":");
String ftp = parts3[0];
String port = parts3[1];

Do you have another solution?

Thank you :)

Maxdola
  • 1,562
  • 2
  • 10
  • 29
Nitneuq
  • 99
  • 3
  • 12
  • 2
    Well, you _could_ use a regex along with capturing groups (which might be optional) and get those. For doing this have a look at how to use `Pattern` and `Matcher`. - The regex itself could look like this (right off the top of my head so it might not be ideal and might contain errors - it should get you started though):`[^:]+://([^:]+):([^@])+@([^:]+):([^/]+)` - group 1 would be the user, group 2 the password, group 3 the domain and group 4 the port. – Thomas Jan 09 '20 at 13:33
  • 3
    Sounds like you'd want to use [java.net.URI](https://docs.oracle.com/javase/7/docs/api/java/net/URI.html) – Filburt Jan 09 '20 at 13:37
  • If you are new using regex you can try using this page https://regexr.com/ it helps a lot to understand how regex works – Jorge Arguedas Arrieta Jan 09 '20 at 13:53

2 Answers2

6

Probably not the cleanest way, but this is just with one split.

String fullFtpAddress = "ftp://user:password@ftp.domain.com:21";
String[] parts = fullFtpAddress.split("[:,@,/]");
String domain = parts[5];
String user = parts[3];
String password = parts[4];
String port = parts[6];

Using Uri:

URI uri = URI.create(fullFtpAddress);
String domain = uri.getHost();
String userInfo = uri.getUserInfo();
String user = userInfo != null ? userInfo.split(":")[0] : null;    
String password = userInfo != null ? userInfo.split(":")[1]: null;
int port = uri.getPort();
lugiorgi
  • 473
  • 1
  • 4
  • 12
Maxdola
  • 1,562
  • 2
  • 10
  • 29
3

Use URL fields:

URL url = new URL("ftp://user:password@ftp.domain.com:21");
String userInfo = url.getUserInfo();
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
...
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • This is only partly what he asked for. – Maxdola Jan 09 '20 at 13:59
  • @Maxdola as I entered the answer the earlier answer was already accepted; a link provided. Added some getters, but not all handled; userInfo can be null, and should be split in name and password. – Joop Eggen Jan 09 '20 at 14:02
  • Ok I see, userInfo can be null, but won't be null since he provided the url which has a userInfo. – Maxdola Jan 09 '20 at 14:04
  • @Maxdola yes. `String user = userInfo == null ? null : userInfo.split(":")[0];` - Still a split. – Joop Eggen Jan 09 '20 at 14:06