给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."。 示例 1:
输入:address = "1.1.1.1"
输出:"1[.]1[.]1[.]1"
var defangIPaddr = function(address) {
return address.replaceAll('.', '[.]')
};
function defangIPaddr(address: string): string {
return address.replace(/\./g, '[.]')
};
func defangIPaddr(address string) string {
return strings.ReplaceAll(address, ".", "[.]")
}
class Solution {
function defangIPaddr($address) {
return preg_replace('/\./', '[.]', $address);
}
}
class Solution {
public String defangIPaddr(String address) {
return address.replace(".", "[.]");
}
}
class Solution:
def defangIPaddr(self, address: str) -> str:
return address.replace('.', '[.]')
char * defangIPaddr(char * address){
int n = strlen(address), j = 0;
char * r = (char *)malloc(sizeof(char) * (n + 7));
for (int i = 0; i < n; i++) {
if (address[i] == '.') {
j += sprintf(r + j, "%s", "[.]");
} else {
r[j++] = address[i];
}
}
r[j] = '\0';
return r;
}
class Solution {
public:
string defangIPaddr(string address) {
regex reg("\\.");
return regex_replace(address, reg, "[.]");
}
};
public class Solution {
public string DefangIPaddr(string address) {
return address.Replace(".", "[.]");
}
}
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
var replaceSpace = function(s) {
return s.replaceAll(' ', '%20');
};
function replaceSpace(s: string): string {
return s.replace(/\s/g, '%20')
};
func replaceSpace(s string) string {
return strings.Replace(s, " ", "%20", len(s));
}
class Solution {
function replaceSpace($s) {
return str_replace(' ', '%20', $s);
}
}
class Solution {
public String replaceSpace(String s) {
return s.replace(" ", "%20");
}
}
class Solution:
def replaceSpace(self, s: str) -> str:
return s.replace(' ', '%20')
char* replaceSpace(char* s){
int n = strlen(s), j = 0;
char * r = (char *)malloc(sizeof(char) * 10001);
for (int i = 0; i < n; i++) {
if (s[i] == ' ') {
j += sprintf(r + j, "%s", "%20");
} else {
r[j++] = s[i];
}
}
r[j] = '\0';
return r;
}
class Solution {
public:
string replaceSpace(string s) {
regex reg("\\s");
return regex_replace(s, reg, "%20");
}
};
public class Solution {
public string ReplaceSpace(string s) {
return s.Replace(" ", "%20");
}
}