strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
strstr(str1,str2)返回的是str2在str1中首次出现的地址,也就是说,strstr(str1,str2)返回值能随str1变化而变化,因为他们内容有共用地址,地址一样,输出的内容也一样。因而,在使用或者处理strstr(str1,str2)返回值之前,切记不要对str1字符串进行更改,若要更改,应该等使用完返回值后再更改!!!
1. #include <stdio.h> 2. #include <stdlib.h> 3. #include <string.h> 4. int main(int argc, char **argv) 5. { 6. char str2[10]= "cdef"; 7. char str1[10]= "abcdefgh"; 8. printf("%s",strstr(str1,str2)); 9. printf("\n"); 10. //返回:cdefgh 11. char str3[10] = "cxef"; 12. printf("%s",strstr(str1,str3)); 13. printf("\n"); 14. //返回:(null) 15. return 0; 16. }
如何使用sscanf分析字符串:
1. #include <stdio.h> 2. #include <stdlib.h> 3. #include <string.h> 4. static void sscanf_test(void); 5. static void sscanf_test(void) 6. { 7. int ret; 8. char *string; 9. int digit; 10. char buf1[255]; 11. char buf2[255]; 12. char buf3[255]; 13. char buf4[255]; 14. 15. /*1.最简单的用法*/ 16. string = "china beijing 123"; 17. ret = sscanf(string, "%s %s %d", buf1, buf2, &digit); 18. printf("1.string=%s\n", string); 19. printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit); 20. /* 21. **执行结果: 22. **1.string=china beijing 123 23. **1.ret=3, buf1=china, buf2=beijing, digit=123 24. **可以看出,sscanf的返回值是读取的参数个数 25. */ 26. 27. /*2.取指定长度的字符串*/ 28. string = "123456789"; 29. sscanf(string, "%5s", buf1); 30. printf("2.string=%s\n", string); 31. printf("2.buf1=%s\n\n", buf1); 32. /* 33. **执行结果: 34. **2.string=123456789 35. **2.buf1=12345 36. */ 37. 38. /*3.取到指定字符为止的字符串*/ 39. string = "123/456"; 40. sscanf(string, "%[^/]", buf1); 41. printf("3.string=%s\n", string); 42. printf("3.buf1=%s\n\n", buf1); 43. /* 44. **执行结果: 45. **3.string=123/456 46. **3.buf1=123 47. */ 48. 49. /*4.取到指定字符集为止的字符串*/ 50. string = "123abcABC"; 51. sscanf(string, "%[^A-Z]", buf1); 52. printf("4.string=%s\n", string); 53. printf("4.buf1=%s\n\n", buf1); 54. /* 55. **执行结果: 56. **4.string=123abcABC 57. **4.buf1=123abc 58. */ 59. 60. /*5.取仅包含指定字符集的字符串*/ 61. string = "0123abcABC"; 62. sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3); 63. printf("5.string=%s\n", string); 64. printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); 65. /* 66. **执行结果: 67. **5.string=0123abcABC 68. **5.buf1=0123, buf2=abc, buf3=ABC 69. */ 70. 71. /*6.获取指定字符中间的字符串*/ 72. string = "ios<android>wp7"; 73. sscanf(string, "%*[^<]<%[^>]", buf1); 74. printf("6.string=%s\n", string); 75. printf("6.buf1=%s\n\n", buf1); 76. /* 77. **执行结果: 78. **6.string=ios<android>wp7 79. **6.buf1=android 80. */ 81. 82. /*7.指定要跳过的字符串*/ 83. string = "iosVSandroid"; 84. sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2); 85. printf("7.string=%s\n", string); 86. printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2); 87. /* 88. **执行结果: 89. **7.string=iosVSandroid 90. **7.buf1=ios, buf2=android 91. */ 92. 93. /*8.分割以某字符隔开的字符串*/ 94. string = "android-iphone-wp7"; 95. /* 96. **字符串取道'-'为止,后面还需要跟着分隔符'-', 97. **起到过滤作用,有点类似于第7点 98. */ 99. sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3); 100. printf("8.string=%s\n", string); 101. printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); 102. /* 103. **执行结果: 104. **8.string=android-iphone-wp7 105. **8.buf1=android, buf2=iphone, buf3=wp7 106. */ 107. 108. /*9.提取邮箱地址*/ 109. string = "Email:beijing@sina.com.cn"; 110. sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4); 111. printf("9.string=%s\n", string); 112. printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4); 113. /* 114. **执行结果: 115. **9.string=Email:beijing@sina.com.cn 116. **9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn 117. */ 118. 119. /*10.过滤掉不想截取或不需要的字符串--补充, 120. **在%号后面加一*号,代表过滤这个字符串,不读取 121. */ 122. string = "android iphone wp7"; 123. sscanf(string, "%s %*s %s", buf1, buf2); 124. printf("10.string=%s\n", string); 125. printf("10.buf1=%s, buf2=%s\n\n", buf1, buf2); 126. /* 127. **执行结果: 128. **10.string=android iphone wp7 129. **10.buf1=android, buf2=wp7 130. */ 131. } 132. int main(int argc, char **argv) 133. { 134. sscanf_test(); 135. return 0; 136. }