c++字符串函数

// MathFuncsDll.cpp
// 
#include "MyStringFunc.h"
#include <stdexcept>
#include <string>
#include <stdio.h>
#include <stdlib.h>

namespace StringFuncs
{
	SString MyStringFunc::RemoveStringSpcaeChar( const SString& strIn )
	{
		const char ansiSpace = ' ';
		const char doubleSpace[3] = "  ";
		SString strMid;
		SString strOut;
		SString::size_type sB    = 0;
		SString::size_type sE    = strIn.find_first_of(ansiSpace);
		SString::size_type sLen  = strIn.size();
		do 
		{
			if (sE == SString::npos)
			{
				strMid += strIn.substr(sB);
				break;
			}
			strMid += strIn.substr(sB,sE-sB);
			sE += 1;
			if (sE >= sLen)
			{
				break;
			}
			sB = sE;
			sE = strIn.find(ansiSpace,sB);
		} while (true);
		sE    = 0;
		sLen  = strMid.size();
		do 
		{
			if (sE + 2 > sLen)
			{
				strOut += strMid.substr(sE);
				break;
			}
			SString strOp = strMid.substr(sE,2);
			if (strOp.at(0) != doubleSpace[0]||strOp.at(1) != doubleSpace[1])
			{
				strOut += strMid.substr(sE,2);
			}
			sE += 2;
		} while (true);
		return strOut;
	}

	bool MyStringFunc::IsInt( const SString& strIn, int nBase )
	{
       bool bResult;
	   int  nLen = strIn.size();
	   char* pStr = (char*) strIn.c_str();
	   bResult = (nLen>0)&&!isspace(pStr[0]);
	   if (bResult)
	   {
		   char* endp;
		   strtol(pStr,&endp,nBase);
		   bResult = (endp - pStr == nLen);
	   }
	   return bResult;
	}

	bool MyStringFunc::IsFloat( const SString& strIn )
	{
		bool bResult;
		int  nLen = strIn.size();
		char* pStr = (char*) strIn.c_str();
		bResult = (nLen>0)&&!isspace(pStr[0]);
		if (bResult)
		{
			char* endp;
			strtod(pStr,&endp);
			bResult = (endp - pStr == nLen);
		}
		return bResult;
	}

	SString MyStringFunc::Trim( SString& strIn )
	{
		if (strIn.empty())
        {
        return strIn;
		}
		strIn.erase(strIn.find_last_not_of(" \t\r\n")+1);
		strIn.erase(0,strIn.find_first_not_of(" \t\r\n"));
		return strIn;
	}

}
 

// MathFuncsDll.h #pragma once #include <string>

namespace StringFuncs {  typedef std::string SString;

 class MyStringFunc  {  public:   // Returns string with spcae   static __declspec(dllexport) SString RemoveStringSpcaeChar(const SString& strIn);      // Returns Bool is Int         static __declspec(dllexport) bool IsInt(const SString& strIn, int nBase);   // Returns Bool is Float         static __declspec(dllexport) bool IsFloat(const SString& strIn);   // Returns string with no spcae   static __declspec(dllexport) SString Trim(SString& strIn);  }; }

 

 

// MyExecRefsDll.cpp
//

#include <iostream>
#include "MathFuncsDll.h"
#include "MyStringFunc.h"
using namespace std;
int main()
{
  str1 = StringFuncs::MyStringFunc::RemoveStringSpcaeChar(str1);
 cout<< str1 <<endl;
 string str2 ="123. 01";
 bool bo1 = StringFuncs::MyStringFunc::IsInt(str2,10);
 bool bo2 = StringFuncs::MyStringFunc::IsFloat(str2);

 if (bo2)
 {
  cout<<1.10<<endl;
 }
 system("pause");
 return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值