I was just confronted with the following question:
How do I determine the count of characters contained in a String?
strlen() does not work, as it returns the len in byte (e.g. strlen("Ä") is 2) .
Any Idea?
best Regards
How to determine length of a string?
- Gertjan van Schijndel
- Posts:634
- Joined: Mon Aug 02, 2010 10:37 am
Re: How to determine length of a string?
To get a real solution to this problem, you will need to report this to your WinCC OA support contact(s).
As workaround you could iterate over the string with the 'string strwalk(string text, int &pos)' function. The pos argument is updated with the position of the next character.
As workaround you could iterate over the string with the 'string strwalk(string text, int &pos)' function. The pos argument is updated with the position of the next character.
- fmulder
- Posts:330
- Joined: Wed Feb 03, 2010 9:46 am
Re: How to determine length of a string?
strlen() works great and does exactly what the 3.12(!) helpfile says. The helpfile says 'length in bytes'.
What you really want is to know the length in utf characters. Which would have been 1
Unfortunately, there is no function for that (yet?)
When you create a new project then you get the option for 'other language' which includes the 'old scool' Ascii. We ran into the same thing and then decide to stick with Ascii for now.
Although we see the power of the utf characters, we were afraid that it would take too much time to go through our existing libraries.
Good luck !
What you really want is to know the length in utf characters. Which would have been 1
Unfortunately, there is no function for that (yet?)
When you create a new project then you get the option for 'other language' which includes the 'old scool' Ascii. We ran into the same thing and then decide to stick with Ascii for now.
Although we see the power of the utf characters, we were afraid that it would take too much time to go through our existing libraries.
Good luck !
- SudoBen
- Posts:11
- Joined: Fri Apr 08, 2011 9:25 am
Re: How to determine length of a string?
@Frenk: Thats exactly what my Question stated before. But I appreciate that you point out that this function actually IS covered by the help :laugh: :laugh:
Seriously: I can't see why it would be so incredibly hard to add such simplest functionality to the code base. It's like missing functions to check if a String coud be cast to an Integer.... oh... :ohmy:
@Gertjan van Schijndel: Thanks for the help. Didn't know about that function yet. I will have a closer look at it (as i have found no documentation about it).
In the meanwhile I've come up with the following solution :
Seriously: I can't see why it would be so incredibly hard to add such simplest functionality to the code base. It's like missing functions to check if a String coud be cast to an Integer.... oh... :ohmy:
@Gertjan van Schijndel: Thanks for the help. Didn't know about that function yet. I will have a closer look at it (as i have found no documentation about it).
In the meanwhile I've come up with the following solution :
Code: Select all
//Returns the actual length (amount of characters)of a string
int StrLen(string s)
{
int clen, len,pos;
clen =1;// first step
char c;
while (clen>0)
{
c=s[pos];
clen=getUTFLength(c);
if (clen>0)
{// Character recognized--> count
len++;
pos+=clen; //step to next character.
}
}
return len;
}
//Determines the length of an utf 32 character.
int getUTFLength(char c)
{
char one = 128; //10000000
char two = 192; //11000000
char three=224; //11100000
char four =240; //11110000
char eos =0; //00000000
if ((c & four) ==four)
{
return 4;
}
if ((c & three) ==three)
{
return 3;
}
if ((c & two) ==two)
{
return 2;
}
if ((c & one) ==one) //this is just part of a char
{
return -1;
}
if ((c|eos)==eos)
{
return 0;
}
if (c- jfily
- Posts:20
- Joined: Wed Feb 05, 2014 10:59 am
Re: How to determine length of a string?
Hi guys.
I made it in few seconds, hope it will help.
(not sure it works in all case, but i tried with some non-ascii char)
I made it in few seconds, hope it will help.
(not sure it works in all case, but i tried with some non-ascii char)
Code: Select all
int _strlen(string str) {
int length = 0;
for(int i = 0; i < strlen(str); i++) {
if(str[i] > 0x7F) {
i++;
}
length++;
}
return length;
}- SudoBen
- Posts:11
- Joined: Fri Apr 08, 2011 9:25 am
Re: How to determine length of a string?
Its a really cool approach but, unfortunately your solution does not work. :woohoo:
You get false results with characters that use more than 2 Bytes, as you miss to check if the following bytes belong to the same character (e.g. "€" will return 2 as it is encoded as 3 Byte Char).
I' think its a great approach (as it is shorter and more generic) but you need to fix it to something like :
You get false results with characters that use more than 2 Bytes, as you miss to check if the following bytes belong to the same character (e.g. "€" will return 2 as it is encoded as 3 Byte Char).
I' think its a great approach (as it is shorter and more generic) but you need to fix it to something like :
Code: Select all
int _strlen(string str) {
int length = 0;
for(int i = 0; i < strlen(str); i++) {
while((str[i+1] & 0xC0)==0x80&&i