Say I had the following code:
说我有以下代码:
char url[63] = {'\0'};
scanf("%s", url);
And the user is asked to submit a url. I need to remove the characters that are typically at the beginnings of url such as http://, ftp://, etc...
并要求用户提交网址。我需要删除通常位于url开头的字符,例如http://,ftp://等...
I could check for the existence of "://" in the character array using a for loop like so:
我可以使用for循环检查字符数组中是否存在“://”:
int i;
for (i=0;i<strlen(url);i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == "/") {
// Super cool code here...
}
}
But say I wanted to delete the :// and everything that came before it? How would I accomplish that? So that if the user entered:
但是说我想删除://以及之前的所有内容?我怎么做到这一点?因此,如果用户输入:
The output would be:
输出将是:
www.google.com
And a similar result if ftp:// were used.
如果使用ftp://,则会得到类似的结果。
3 个解决方案
#1
1
All you need is:
所有你需要的是:
int i;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
return &url[i+3];
}
}
To create a copy:
要创建副本:
char* getInteresting(char* url)
{
int i = 0;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
int len = strlen(url) - (i+2);
char* copy = (char*)malloc(len + 1);
strcpy(copy, url + i + 3);
return copy;
}
}
}
Plus: a lot more checking through the error cases!
另外:通过错误案例检查更多!
#2
1
Usually the easiest way to do this sort of thing in C is to not actually change the original array at all, but instead to create another char *
pointer to the first part of the string you actually care about. So....
通常,在C中执行此类操作的最简单方法是实际上根本不更改原始数组,而是创建另一个char *指针,指向您实际关注的字符串的第一部分。所以....
int i;
char *interesting_stuff;
for (i=0;i<strlen(url);i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == "/") {
interesting_stuff = url + i + 3;
}
}
and then go on to do things with interesting_stuff
and leave url
alone. (Be aware that it's a pointer into the same array, so if you overwrite url
, you will lose interesting_stuff
too.)
然后继续使用interesting_stuff做事并留下url。 (请注意,它是指向同一个数组的指针,因此如果您覆盖url,您也将失去interesting_stuff。)
BTW, the bounds on that for
loop will get you in trouble. You potentially look two characters past the end of the string, and my modification makes that problem a bit worse. You need to check the length of the string first and be sure that you don't go past the point in the string where ://
followed by some useful data could be found. In other words, you want to constrain your loop to strlen(url) - 4
except be careful if url
is shorter than 4 characters.
顺便说一下,for循环的界限会让你遇到麻烦。您可能会在字符串末尾看到两个字符,而我的修改会使问题变得更糟。您需要首先检查字符串的长度,并确保不要超过字符串中的点://后跟一些有用的数据。换句话说,您希望将循环约束为strlen(url) - 4,但小心如果url短于4个字符。
#3
0
const char* ssc = strstr(url, "//:");
if (ssc)
{
const char* withoutProtocol = ssc + 3;
// do something with withoutProtocol
}
#1
1
All you need is:
所有你需要的是:
int i;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
return &url[i+3];
}
}
To create a copy:
要创建副本:
char* getInteresting(char* url)
{
int i = 0;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
int len = strlen(url) - (i+2);
char* copy = (char*)malloc(len + 1);
strcpy(copy, url + i + 3);
return copy;
}
}
}
Plus: a lot more checking through the error cases!
另外:通过错误案例检查更多!
#2
1
Usually the easiest way to do this sort of thing in C is to not actually change the original array at all, but instead to create another char *
pointer to the first part of the string you actually care about. So....
通常,在C中执行此类操作的最简单方法是实际上根本不更改原始数组,而是创建另一个char *指针,指向您实际关注的字符串的第一部分。所以....
int i;
char *interesting_stuff;
for (i=0;i<strlen(url);i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == "/") {
interesting_stuff = url + i + 3;
}
}
and then go on to do things with interesting_stuff
and leave url
alone. (Be aware that it's a pointer into the same array, so if you overwrite url
, you will lose interesting_stuff
too.)
然后继续使用interesting_stuff做事并留下url。 (请注意,它是指向同一个数组的指针,因此如果您覆盖url,您也将失去interesting_stuff。)
BTW, the bounds on that for
loop will get you in trouble. You potentially look two characters past the end of the string, and my modification makes that problem a bit worse. You need to check the length of the string first and be sure that you don't go past the point in the string where ://
followed by some useful data could be found. In other words, you want to constrain your loop to strlen(url) - 4
except be careful if url
is shorter than 4 characters.
顺便说一下,for循环的界限会让你遇到麻烦。您可能会在字符串末尾看到两个字符,而我的修改会使问题变得更糟。您需要首先检查字符串的长度,并确保不要超过字符串中的点://后跟一些有用的数据。换句话说,您希望将循环约束为strlen(url) - 4,但小心如果url短于4个字符。
#3
0
const char* ssc = strstr(url, "//:");
if (ssc)
{
const char* withoutProtocol = ssc + 3;
// do something with withoutProtocol
}