Le 4 mai 2021, la plateforme Yahoo Questions/Réponses fermera. Elle est désormais accessible en mode lecture seule. Aucune modification ne sera apportée aux autres sites ou services Yahoo, ni à votre compte Yahoo. Vous trouverez plus d’informations sur l'arrêt de Yahoo Questions/Réponses et sur le téléchargement de vos données sur cette page d'aide.
Java: String and using the split function?
OK, Im trying to split a string containing an IP Address into the 4 byte address.
Here is my code:
getIP(String st){ //st contains "192.168.1.21"
String[] var = st.split(".");
...
After the split, var has a length of 0. WHY???
If I change it to:
String[] var = st.split("1");
...
I get what I would expect with var having a length of 4 and each string containing:
var[0] = ""
var[1] = "92."
var[2] = "68."
var[3] = ".2"
Can you not use the dot (.) character to split a string???
3 réponses
- larrybud2004Lv 6il y a 7 ansRéponse favorite
Split works fine, but it's expecting a regular expression. Since the "." in RegEx is a special character, you have to escape it.
BUT since "\" has a special meaning in a string, you have to escape it TOO SO
String[] var = st.split("\\.");
- SadsongsLv 7il y a 7 ans
I've never used split (it's a while since I wrote any Java) but as it takes a regexp, you'd have to escape the . I would say.