Search code examples
stringmojolang

String manipulation in Mojo


I have a Mojo struct with the following initialiser:

    fn __init__(inout self, value: String):
        """Constructor."""
        self.value = value.lower()

If I have something as simple as a String, and the "lower()" function exists in Python on the str object, but that functionality does not exist in Mojo, what is the correct way forward? Roll my own lower() in Mojo, or pull it somehow from Python?

Any pointers would be much appreciated.


Solution

  • One way forward could be to import this functionality from Python. I don't know if this is the most elegant way, but it works...:

    fn __init__(inout self, value: String) raises:
        self.value = PythonObject(value).lower().to_string()