LeetCode.IP address invalidation (Java)

Give you a valid IPv4 address, and return the invalidated version of this IP address.
The so-called invalidation of the IP address is actually replacing each "." with "[.]".

The solution is very simple:
one, you can choose to adjust the library

public String defangIPaddr(String address) {
    
    
	return address.replace(".", "[.]");
}

2. Replace the "." part with "[.]" and join them together

public String defangIPaddr2(String address) {
    
    
	String res = "";
	for (int i = 0; i < address.length(); i++) {
    
    
		if (address.charAt(i) == '.')
			res += "[.]";
		else
			res += address.charAt(i);
	}
	return res;
}

end.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/108573504