str!=null与str!=string.Empty有什么区别

str!=null与str!=string.Empty有什么区别
这样可以吧?有必要吗?
if(str!=null&&str!=string.Empty)
   ......
还是只要做其中一个判断就可以了? 

有必要
str!=null 判断str是否已经初始化,或者说是否给它赋值
str!=string.Empty 判断str的值是否为空,相当于 str!=""

实际上String.Empty  指向的内存区域包含一个字符  '/0 '  , 而null则还没分配内存,所以是不相同的

如果用的是C#2.0,建议你用这个函数
String.IsNullOrEmpty(param)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是修改后的程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct String { char *data; int length; }; void initString(struct String *S) { S->data = NULL; S->length = 0; } void assignString(struct String *S, char *str) { int len = strlen(str); if (len == 0) { if (S->data != NULL) { free(S->data); S->data = NULL; S->length = 0; } } else { if (S->data != NULL) { free(S->data); } S->data = (char *)malloc(sizeof(char) * (len + 1)); strcpy(S->data, str); S->length = len; } } void printString(struct String *S) { if (S->data == NULL) { printf("Empty String\n"); } else { printf("%s\n", S->data); } } void getNext(struct String *P, int *next) { int i = 0, j = -1; next[0] = -1; while (i < P->length) { if (j == -1 || P->data[i] == P->data[j]) { i++; j++; next[i] = j; } else { j = next[j]; } } } int KMP(struct String *T, struct String *P) { int i = 0, j = 0; int *next = (int *)malloc(sizeof(int) * (P->length + 1)); getNext(P, next); while (i < T->length && j < P->length) { if (j == -1 || T->data[i] == P->data[j]) { i++; j++; } else { j = next[j]; } } free(next); if (j == P->length) { return i - j; } else { return -1; } } int main() { struct String T, P; initString(&T); initString(&P); assignString(&T, "ababcabcacbab"); assignString(&P, "abcac"); printf("T: "); printString(&T); printf("P: "); printString(&P); int pos = KMP(&T, &P); if (pos != -1) { printf("模式串在主串中的位置是:%d\n", pos); } else { printf("未找到模式串!\n"); } return 0; } ``` 修改的地方如下: 1. 在 `initString` 函数中,将 `S->data` 初始化为 `NULL`。 2. 在 `assignString` 函数中,添加对空字符串的处理。 3. 在 `printString` 函数中,添加换行符。 4. 在 `main` 函数中,给字符串 `T` 和 `P` 赋初值,并且在输出时添加换行符。在输出模式串位置时,也添加了换行符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值