Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting Characters in Strings

Subject: Counting Characters in Strings

From: hansenra@erim-int.com

Date: 3 Nov, 1997 14:35:52

Message: 1 of 4

Is there a way to count how many characters are in a string?

Tony Hansen
Erim International, Inc.

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/ Search, Read, Post to Usenet

Subject: Counting Characters in Strings

From: garyl@mathworks.com (Gary Levenson)

Date: 3 Nov, 1997 21:19:22

Message: 2 of 4

[Posted and mailed]

In article <878588960.15040@dejanews.com>,
hansenra@erim-int.com writes:
> Is there a way to count how many characters are in a string?

>> str = 'howlongami';
>> length(str)

len =

 10


HTH.

===========================================
Gary Levenson gary@mathworks.com
The MathWorks, Inc. ph: 508 647-7000
         http://www.mathworks.com
===========================================

Subject: Counting Characters in Strings

From: woodchips@worldnet.att.net (John D'Errico)

Date: 3 Nov, 1997 20:57:30

Message: 3 of 4

In article <878588960.15040@dejanews.com>, hansenra@erim-int.com wrote:

> Is there a way to count how many characters are in a string?

The obvious answer,

   count=len(str);

gives the total number of characters, including spaces. If
your question was actually how many non-blank characters
in the string, then you might try:

   count=sum(str~=' ');

Or, if what you are asking is how many alphabetic characters
there are in the string, then you might try:

   alpha='abcdefghijklmnopqrstuvwxyz';
   count=sum(ismember(lower(str),alpha));

Enjoy,
John D'Errico
derrico@kodak.com

--

John D'Errico
derrico@kodak.com

Subject: Counting Characters in Strings

From: michaels@bigdog.engr.arizona.edu (Elvis Presley)

Date: 5 Nov, 1997 00:53:03

Message: 4 of 4

of course, if you have a padded string, then

   length(deblank(str))

is a good way.

John D'Errico (woodchips@worldnet.att.net) wrote:
: In article <878588960.15040@dejanews.com>, hansenra@erim-int.com wrote:

: > Is there a way to count how many characters are in a string?

: The obvious answer,

: count=len(str);

: gives the total number of characters, including spaces. If
: your question was actually how many non-blank characters
: in the string, then you might try:

: count=sum(str~=' ');

: Or, if what you are asking is how many alphabetic characters
: there are in the string, then you might try:

: alpha='abcdefghijklmnopqrstuvwxyz';
: count=sum(ismember(lower(str),alpha));

: Enjoy,
: John D'Errico
: derrico@kodak.com

: --

: John D'Errico
: derrico@kodak.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us