荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: bakey (深海的鱼爱上会潜水的猫), 信区: Program
标  题: [合集]请教c问题
发信站: 荔园晨风BBS站 (2005年05月31日12:57:18 星期二), 站内信件

☆   1  ──────────── 我是分割线 ─────────────────☆
发信人: Bambooflute (男儿当自强), 信区: Program
标  题: 请教c问题
时  间: 2005年04月12日10:49:53 星期二

c或c++中有没有取子串函数substr()?如果没有,有没有其他函数可以实现
这种功能?



☆   2  ──────────── 我是分割线 ─────────────────☆
发信人: contonazhao ([MU]-9-萨肥), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日11:06:55 星期二

(from MSDN)
Description
Returns a substring beginning at a specified location and having a
specified length.
Syntax
stringvar.substr(start [, length ])
The substr method syntax has these parts:

Part Description
stringvar Required. A string literal or String object from which the
substring is extracted.
start Required. The starting position of the desired substring. The
index of the first character in the string is zero.
length Optional. The number of characters to include in the returned
substring.

Remarks
If length is zero or negative, an empty string is returned. If not
specified, the substring continues to the end of stringvar.
The following example illustrates the use of the substr method:

function SubstrDemo()
{
  var s, ss;
  var s = "The quick brown fox jumped over the lazy yellow dog.";
  ss = s.substr(16, 3);
  // Returns "fox"
  return(ss);
}




☆   3  ──────────── 我是分割线 ─────────────────☆
发信人: Bambooflute (男儿当自强), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日11:22:04 星期二

头函数是什么呢?还是string吗




☆   4  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日11:29:10 星期二

应该是用strncpy吧,
dst应该是一个大小 >= length + 1 char 数组。

substr(dst, src, start, length)  {
 strncpy(dst, src + start, length);
}

c或c++中有没有取子串函数substr()?如果没有,有没有其他函数可以实现
这种功能?



☆   5  ──────────── 我是分割线 ─────────────────☆
发信人: Bambooflute (男儿当自强), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日11:40:46 星期二

good!可以哦,非常谢谢。顺便问问如何删除字符串中的某个字符啊




☆   6  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日11:59:57 星期二

字符串就是一个char数组, 就是把其中一个元素删掉,再把后面的元素全部移前一位就
可以了。




☆   7  ──────────── 我是分割线 ─────────────────☆
发信人: Bambooflute (男儿当自强), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日12:10:01 星期二

这样会比较麻烦,有专门的函数么




☆   8  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日12:12:59 星期二

你写了就有了,就算有这么个函数,它里面的原理也是差不多一样麻烦的.




☆   9  ──────────── 我是分割线 ─────────────────☆
发信人: Bambooflute (男儿当自强), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日12:15:34 星期二

好的,先用这个实现一下,谢谢




☆  10  ──────────── 我是分割线 ─────────────────☆
发信人: mmkiller (我有PERM_NoBirdYou), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日12:46:52 星期二

strstr




☆  11  ──────────── 我是分割线 ─────────────────☆
发信人: mmkiller (我有PERM_NoBirdYou), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日14:35:07 星期二

Returns a pointer to the first occurrence of a search string in a string.

char *strstr(
   const char *string,
   const char *strSearch
);
wchar_t *wcsstr(
   const wchar_t *string,
   const wchar_t *strSearch
);
unsigned char *_mbsstr(
   const unsigned char *string,
   const unsigned char *strSearch
);
Parameters
string
Null-terminated string to search.
strSearch
Null-terminated string to search for.
Return Value
Returns a pointer to the first occurrence of strSearch in string, or NULL if
strSearch does not appear in string. If strSearch points to a string of zero
length, the function returns string.

Remarks
The strstr function returns a pointer to the first occurrence of strSearch in
string. The search does not include terminating null characters. wcsstr and
_mbsstr are wide-character and multibyte-character versions of strstr. The
arguments and return value of wcsstr are wide-character strings; those of
_mbsstr are multibyte-character strings. These three functions behave
identically otherwise.

Security Note    These functions incur a potential threat brought about by a
buffer overrun problem. Buffer overrun problems are a frequent method of
system attack, resulting in an unwarranted elevation of privilege. For more
information, see Avoiding Buffer Overruns.
Generic-Text Routine Mappings

TCHAR.H routine  _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcsstr strstr  _mbsstr  wcsstr

Requirements
Routine Required header Compatibility
strstr <string.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
wcsstr <string.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_mbsstr <mbstring.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the
Introduction.

Libraries

All versions of the C run-time libraries.

Example
// crt_strstr.c

#include <string.h>
#include <stdio.h>

char str[] =    "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

int main( void )
{
   char *pdest;
   int  result;
   printf( "String to be searched:\n   %s\n", string );
   printf( "   %s\n   %s\n\n", fmt1, fmt2 );
   pdest = strstr( string, str );
   result = (int)(pdest - string + 1);
   if ( pdest != NULL )
      printf( "%s found at position %d\n", str, result );
   else
      printf( "%s not found\n", str );
}
Output
String to be searched:
   The quick brown dog jumps over the lazy fox
            1         2         3         4         5
   12345678901234567890123456789012345678901234567890

lazy found at position 36
See Also
String Manipulation Routines | strcspn | strcmp | strpbrk | strrchr | strspn
| Run-Time Routines and .NET Framework Equivalents

-------------------------------------------------------------------------------

Send feedback on this topic to Microsoft

? Microsoft Corporation. All rights reserved.

】 : : c或c++中有没有取子串函数substr()?如果没有,有没有其他函数可以实现



☆  12  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:08:43 星期二

mm, strstr是查找字串的,他要的应该是载取子串的



☆  13  ──────────── 我是分割线 ─────────────────☆
发信人: mmkiller (我有PERM_NoBirdYou), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:10:23 星期二

 what 截取?




☆  14  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:13:11 星期二


大意是要得到某个字段的第N个字符开始的一个长度为M的子字符串,
如要取得mmkiller的第0个字符开始的长度为2的字段,那就是mm了.




☆  15  ──────────── 我是分割线 ─────────────────☆
发信人: mmkiller (我有PERM_NoBirdYou), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:14:54 星期二

 可以取得开始地址然后用copymemory啊




☆  16  ──────────── 我是分割线 ─────────────────☆
发信人: jiffies (jiffies), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:16:33 星期二

本来就是用XXXcpy, memcpy or strcpy.




☆  17  ──────────── 我是分割线 ─────────────────☆
发信人: mmkiller (我有PERM_NoBirdYou), 信区: Program
标  题: Re: 请教c问题
时  间: 2005年04月12日15:21:52 星期二

 看不懂你说什么




☆  18  ──────────── 我是分割线 ─────────────────☆
发信人: Whidbey (Whidbey), 信区: Program
标  题: Re: 请教c问题
时  间: Wed Apr 13 17:40:25 2005

strstr()


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店