Questions tagged [strlen]

A standard C function that returns the length of a string.

This function calculates the length of the string, excluding the terminating null byte \0. It is defined in the <string.h> standard header.

Documentation for C strlen.

692 questions
8
votes
5 answers

PHP: strlen returns character length instead of byte length

I have a wordpress web site. I've created simple page template like: Then i've created a page using this template. The page shows the length of russian string 'Привет' (means…
Vasiliy
  • 81
  • 1
  • 2
8
votes
1 answer

Get same result for php `str_len ` as for jQuery `.val().length()`

I use jQuery to count the value of a textarea on the fly: function count_chars () { count_chars=$('#text_textarea').val().length; } ...then on submit serialize the form, send the text of the textarea via ajax to a php file which then validates…
Chris
  • 3,756
  • 7
  • 35
  • 54
7
votes
2 answers

Convert CGSize to CGFloat and use that to size a RoundRectButton

I'm trying to find the physical pixel size of a string of text. I then want to use this size to set the length of a roundRectButton. The method I'm using to get the length however returns a CGSize. How do I convert that to a CGFloat? Alternatively…
ElasticThoughts
  • 3,417
  • 8
  • 43
  • 58
7
votes
3 answers

Why use "strlen30()" instead of "strlen()"?

I've read and wondered about the source code of sqlite static int strlen30(const char *z){ const char *z2 = z; while( *z2 ){ z2++; } return 0x3fffffff & (int)(z2 - z); } Why use strlen30() instead of strlen() (in string.h)??
hority
  • 228
  • 1
  • 8
7
votes
11 answers

Is there a difference between $str == '' and strlen($str) == 0 in PHP?

As the title says: Is there a difference between $str == '' and strlen($str) == 0 in PHP? Is there any real speed difference and is one better to use than the other?
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
7
votes
6 answers

What if a null character is present in the middle of a string?

I understand that the end of a string is indicated by a null character, but i cannot understand the output of the following code. #include #include int main(void) { char s[] = "Hello\0Hi"; printf("%d %d", strlen(s),…
Ranjan Srinivas
  • 347
  • 1
  • 3
  • 10
7
votes
2 answers

C strings, strlen and Valgrind

I'm trying to understand why Valgrind is spitting out : ==3409== Invalid read of size 8 ==3409== at 0x4EA3B92: __GI_strlen (strlen.S:31) whenever I'm applying strlen on a dynamically allocated string? Here is a short testcase : #include…
Lenain
  • 105
  • 1
  • 6
7
votes
4 answers

Is strlen on a string with uninitialized values undefined behavior?

strlen returns the number of characters that precede the terminating null character. An implementation of strlen might look like this: size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return(s - str); } This…
ペニス
  • 73
  • 4
6
votes
3 answers

Counting length of string with HTML numbered entities in PHP

I would like to count the length of a string with PHP. The string contains HTML entity numbers, which inflate the number of characters that are counted: a dash is – which is counted as 7 when I only want it to count as 1. How do I convert the…
Squrler
  • 3,444
  • 8
  • 41
  • 62
6
votes
6 answers

strlen in assembly

I made my own implementation of strlen in assembly, but it doesn't return the correct value. It returns the string length + 4. Consequently. I don't see why.. and I hope any of you do... Assembly source: section .text [GLOBAL stringlen:] ; C…
Michel
  • 113
  • 1
  • 1
  • 5
6
votes
6 answers

Fast strlen with bit operations

I found this code int strlen_my(const char *s) { int len = 0; for(;;) { unsigned x = *(unsigned*)s; if((x & 0xFF) == 0) return len; if((x & 0xFF00) == 0) return len + 1; if((x & 0xFF0000) == 0) return len…
Kevin
  • 1,151
  • 1
  • 10
  • 18
6
votes
2 answers

strlen not giving correct string length C

I am reading from my dictionary and printing out the word + the length of the word for testing purposes. I use strlen to get the length of the string. However, the numbers I got are not correct. I believe strlen doesn't count the \0 character. I am…
PTN
  • 1,658
  • 5
  • 24
  • 54
6
votes
2 answers

how does strlen count unicode in c

I'm curious as to how strlen count unicode characters of multiple bytes in C. Does it count each byte or character (as they can consist of several bytes) until first '\0'?
Horse SMith
  • 1,003
  • 2
  • 12
  • 25
6
votes
9 answers

Quick strlen question

I've come to bother you all with another probably really simple C question. Using the following code: int get_len(char *string){ printf("len: %lu\n", strlen(string)); return 0; } int main(){ char *x = "test"; char y[4] =…
LearningC
  • 85
  • 1
  • 1
  • 5
6
votes
5 answers

How do I find the number of bytes within UTF-8 string with PHP?

I have the following function from the php.net site to determine the # of bytes in an ASCII and UTF-8 string:
Luke
  • 6,195
  • 11
  • 57
  • 85
1 2
3
46 47