String and juce_wchar


#1

How can I create String from juce_wchar?

I'm traversing characters in string and parsing digits.

if ((lineStr.getCharPointer() + i).isDigit())
{
  int digit = *(lineStr.getCharPointer() + i) - '0';
}

Is there a better way to do this?

 

 

 


#2

What are you actually trying to achieve? There are all kinds of helper methods available in JUCE for many different usecases, for example you can simply convert a String to an int using String::getIntValue().


#3

I'm parsing various files. In my case i can not use getIntValue() because I have no way of knowing if conversion was succesful.
int val = juce::String("12345abc").getIntValue(); returns 12345. It should return false. 

Below is my current code which I try to improve:


/*static*/ bool StringUtils::intTryParse(const juce::String& s, int* i)
{
    if (!s.containsOnly("+-0123456789"))
        return false;
    if (s.lastIndexOfChar('+') > 0)
        return false;
    if (s.lastIndexOfChar('-') > 0)
        return false;
    *i = s.getIntValue();
    return true;
}
/*static*/ bool StringUtils::doubleTryParse(const juce::String& s, double* d)
{
    if (!s.containsOnly("+-0123456789."))
        return false;
    if (s.lastIndexOfChar('+') > 0)
        return false;
    if (s.lastIndexOfChar('-') > 0)
        return false;
    if (s.indexOfChar('.') != s.lastIndexOfChar('.'))
        return false;
    *d = s.getDoubleValue();
    return true;
}

 

My other wchar issue is this:

 wchar_t wc[2];
 wc[0] = 0x2013;
 wc[1] = 0;
 juce::String test(wc);

I was expecting to get "‒" but I do not. 

I do get "‒" value with this: code

char ch[2];
ch[0] = -106;
ch[1] = 0;
juce::String::createStringFromData(ch, 2);

 


#4

Try String::charToString()