简体   繁体   中英

How do I split a string in Java8?

I want to split the string "004-034556" into two strings by the delimiter "-" :

part1 = "004";
part2 = "034556";

That means the first string will contain the characters before '-' , and the second string will contain the characters after '-' .

I also want to check if the string has '-' in it.

When you encounter some kind of transient.network issue like this, you should do something sane rather than just aborting your program. Maybe you could wait a minute and then try again.

There are a lot of reasons this could happen. You may just happen to get routed to a server that can't handle your request at that moment. You may be temporarily rate-limited due to Google perceiving your connection storm as abuse. Your Inte.net connection may drop for a second or two. You may run out of local ephemeral ports.

You need to write code to handle these cases, and you haven't.

Also, one fine point. You don't call bind . This forces the connect call to do an implicit bind before the connection can be attempted. This is not good in an application like this for two reasons:

  1. You have no control over the bind parameters. For example, you have no control over the local port.

  2. If the bind fails, you will get an error from connect , making it harder to figure out the right response.

I would strongly urge you to explicitly call bind , choose the local port yourself, and sanely handle bind errors separately from connect errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM