Count characters in a string


#1

I need to count the number of characters in a string.

I’ve tried this code:
“hello”.Length - “hello”.Replace(“l”,"").Length

I tried also:
len(“hello”) - len(“hello”.Replace(“l”,""))

But in both cases I’m getting an error like ’ Option Strict On disallows implicit conversions from ‘Integer’ to ‘string’’

What is the right command to count the length of a string?


#2

“hello”.Split("l"c).Length - 1


#3

I have the same error:


#4

it is missing the conversion to string type (as the formula returns an integer).

in a messagebox will look like:
(“hello”.Split("l"c).Length - 1).ToString


#5

Fantastic, thanks!!


#6

i have tried with below code

“hello”.Split("a"c).Length.tostring

i got result 1. here there is no letter “a” in hello word. then why i am getting result 1.


#7

your result is the count of substrings after splitting “hello” word with “a”, not the occurences of “a” letter.

To get the count you should do “hello”.Split("a"c).Length-1


#8

An alternative Linq solution handling both lower and upper Cases

"UIPATH".Count(Function(c) LCase(c)="a").ToString

Cheers


#9

Hi,vvaidya

How to count of substrings like “hello” in string:" hello,hello world"