string tips

1. wstring 向 char 转化

std::wstring s2 ( L"Hello" );
char *str = new char[255];
sprintf(str,"%ls",s2.c_str());


2. 定义一个 tstring
typedef std::basic_string<TCHAR> tstring;

3. 在 link  tab 之下加入以下,可以查看详细的链接lib的情况
/verbose:lib

4. string to TCHAR
std::string str="something";
TCHAR *param=new TCHAR[str.size()+1];
param[str.size()]=0;
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(str.begin(),str.end(),param);

    5. string.IndexOf 默认是区分大小写的    (C#)
    可以通过参数 System.StringComparison.OrdinalIgnoreCase ,忽略大小写
    另有System.StringComparison.CurrentCultureIgnoreCase等

you could use IndexOf() and pass StringComparison.OrdinalIgnoreCase
string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;

Even better is defining a new extension method for string

public static bool Contains(this string source, string toCheck, StringComparison comp) {
  return source.IndexOf(toCheck, comp) >= 0;
}

string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);

另一个解决方案:
bool contains = Regex.Match("StRiNG to search", "string", RegexOptions.IgnoreCase).Success;

posted @ 2012-01-13 17:20  BiG5  阅读(136)  评论(0编辑  收藏  举报