Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:11:02+00:00 2026-06-14T08:11:02+00:00

Okay, I searched StackOverflow on how to check if a string is empty or

  • 0

Okay, I searched StackOverflow on how to check if a string is empty or just whitespace. But, it only works with ANSI strings. How can I get it to work with a wstring?

Here is the code:

#include <string>
using namespace std;

//! Checks if a string is empty or is whitespace.
bool IsEmptyOrSpace(const string& str) {
    string::const_iterator it = str.begin();

    do {
        if (it == str.end())
            return true;
    } while (*it >= 0 && *it <= 0x7f && isspace(*(it++)));
    // One of these conditions will be optimized away by the compiler.
    // Which one depends on whether the characters are signed or not.

    return false;
}

My first thought was to change isspace(*(it++)) to iswspace(*(it++)), but the two conditions before that will only work with ASCII, right? Here is what I have so far on attempting to adapt the function to wstring‘s:

bool IsEmptyOrSpaceW(const wstring& str) {
    String::const_iterator it = str.begin();

    do {
        if (it == str.end())
            return true;
    } while (*it >= 0 && *it <= 0x7f && iswspace(*(it++)));
    // One of these conditions will be optimized away by the compiler.
    // Which one depends on whether the characters are signed or not.

        // Do I need to change "*it >= 0 && *it <= 0x7f" to something else?

    return false;
}

Is my approach close to being correct? Either way, how can I implement a Unicode version of this IsEmptyOrSpace() function?

EDIT:
Okay, if you need to know why the *it >= 0 && *it <= 0x7f test is there, I cannot tell you, because I do not know. I got the code for the function from the answer to this question: C++ check if string is space or null
So let me start from scratch, how, in general, may I check if a wstring is EMPTY or just whitespace?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:11:03+00:00Added an answer on June 14, 2026 at 8:11 am

    but the two conditions before that will only work with ASCII, right?

    That’s right. They make sure the value conforms to the precondition of isspace: the argument “must have the value of an unsigned char or EOF”. Strictly speaking, you only need the *it >= 0 check, which should be optimised out if char is unsigned; alternatively, as mentioned in the comments, you could convert the value to unsigned.

    iswspace has no such precondition, so just remove those checks from the wide version:

    bool IsEmptyOrSpaceW(const wstring& str) {
        wstring::const_iterator it = str.begin();
    
        do {
            if (it == str.end())
                return true;
        } while (iswspace(*(it++)));
    
        return false;
    }
    

    As a matter of style, there’s no need to add a strange wart like W to indicate the parameter type, since you can just overload IsEmptyOrSpace with different parameter types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is probably a very basic question; but, I'm just getting back in
Okay I have searched and can't seem to come up with where to find
Okay, so I have searched a while for a solution to this problem, but
Okay, this should be really simple, but I have searched all over for the
I have searched everywhere and have yet to get a solution. Okay heres the
I have searched the forum for this , but found no similar question, okay
Okay, so I've searched, and asked on the PayPal forum but no one will
Okay, I have literally searched all over the web, but I didn't find what
Okay, so first, I have searched for this everywhere but it seems like every
okay i give up on strings , i dont know i dont get any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.