PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 11, 2022

[FIXED] How do I compare single multibyte character constants cross-platform?

 September 11, 2022     c++, character-encoding, cross-platform, multibyte     No comments   

Issue

As I am writing a cross-platform application, I want my code to work on both platforms.

In Windows, I use this code to compare 2 single characters.

for( int i = 0;  i < str.size();  i++ )
    if( str.substr( i, 1 ) == std::string( "¶" ) )
        printf( "Found!\n" );

Now, in Linux, the character is not found. It is found when I change the substring to a length of 2.

for( int i = 0;  i < str.size();  i++ )
    if( str.substr( i, 2 ) == std::string( "¶" ) )
        printf( "Found!\n" );

How do convert this character comparison code to be cross platform?


Solution

Solved using str.compare() and size() of character string:

std::string str = "Some string ¶ some text ¶ to see";
std::string char_to_compare = "¶";

for( int i = 0;  i < str.size();  i++ )
    if( str.compare( i, char_to_compare.size(), char_to_compare ) == 0 )
        printf( "Found!\n" );


Answered By - Z0q
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing