String manipulation


I am trying to use ChaiScript to allow users to manipulate strings and pass them back to the app after they have changed. I’m a confused to what string functions work in ChaiScript. Any good example of working with strings anyone can point me to.

  1. How to loop over a string. The [] operator doesn’t seem to work.

  2. What kind of search and find is supported. Would those need to be loaded through functions, passing into the correct std functions?

  3. Any work on supporting unicode strings. I see quite a few posts about it but i’m not seeing it work or how to set them as unicode. (Tolower / toupper for strings?)

How would something like this be accomplished in chaiscript

    std::wstring newPath;
std::wstring somePath = _T("some!path&remove");
for (int i = 0; i < somePath.size(); i++)
	if (somePath[i] != _T('!') &&
		somePath[i] != _T('&'))
		newPath += somePath[i];


ChaiScript strings are simply C++ std::string objects. All (or most) C++ operations should work.

Full unicode support might be hard, utf8 would work, but there’s no support that would let you loop over each character, just over each byte of the string. I have no problem with string[index] working in my tests, so I’m not sure what issue you would be having.

if you want to use wstring you can add it to your own instance of ChaiScript with a command like:


And it should support all the same functions as the regular string.



ok [] is working, not sure what i was looking at.

trying to get the following script running

std::string script = R""(
var newString = ""
var oldString = "some!path&remove"
for (var i = 1; i < oldString.size(); ++i)
if(oldString[i] != ‘!’ &&
oldString[i] != ‘&’)
newString += oldString[i]

	return newString

getting the error "eString = “Error: “Unable to find appropriate’+=’ operator.” With parameters: (string, char) during evaluation”

Can you help with the syntax of adding the += operator function or are there any includes that will define that.

If i include wstring how would that interact with string literals “some string”, they would still be std::string


This is working, is this the correct way to handle this?

chai.add(chaiscript::fun([](std::string &lhs, const char &rhs) -> decltype(auto) {return (lhs += rhs);}), “+=”);

why wouldn’t this have worked by default.


correct, yes, but I would do const char not const char &, but it works either way.

It is a simple oversight that it wasn’t already there. I just added it to develop branch.


Thanks, using the latest from development branch, working great.