Skip to content

Instantly share code, notes, and snippets.

@kauevestena
Last active February 24, 2019 16:13
Show Gist options
  • Save kauevestena/8ed4c5bd1a9e54d5fa20 to your computer and use it in GitHub Desktop.
Save kauevestena/8ed4c5bd1a9e54d5fa20 to your computer and use it in GitHub Desktop.
functions for simple string manipulation
string subsAfLChar(string input,string character)
{
//return the string after the last occur. of the specified character
int pos = input.rfind(character);
if(pos != -1)
{
return input.substr(pos+1,string::npos);
}
else {return "";}
}
string subsAfChar(string input,string character)
{
//return the string after the first occur. of the specified character
return input.substr(input.find(character)+1,string::npos);
}
string changeChar2(string input,string charO,string charD)
{
//change the first occur of a character with another character
return input.replace(input.find(charO),1,charD);
}
string subsBefChar(string input,string character)
{
//return the string before the first occur. of the specified character
return input.substr(0,input.find(character));
}
string subsBefLChar(string input,string character,bool includeChar=false)
{
//return the string before the last occur. of the specified character
int pos = input.rfind(character);
if(pos != -1)
{
if (includeChar)
{
return input.substr(0,pos)+character;
}
else
{return input.substr(0,pos);}
}
else
{return "";}
}
string preffix(int number,int nZeros,bool printNumber)
{
//you have a number, and want an unified preffix
int maxNumb = 10;
string res = "";
for(unsigned int i = 1;i<nZeros;i++)
{
maxNumb *= 10;
}
if (maxNumb < number)
{
return "";
}
int cont = maxNumb;
for(;;)
{
if (cont > number)
{
res+="0";
cont/= 10;
}
else
{break;}
}
//cout<<maxNumb;
if (printNumber)
{
return res+string(number);
}
else
{
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment