## String Character Analysis
In this example we use the operators that allow us to recognise the type of character we are dealing with. It is useful to check if a character is ASCII, or is upper case, or numeric, or it is a punctuation mark and so forth. The options available cover a variety of situations and this is demonstrated in the sketch below. Every character sent to the board through the serial monitor of the Arduino Software (IDE) is analysed by the sketch that returns all the information it was able to find. A single character may trigger more than one condition and therefore you may get multiple answers for a single entry.
The available operators are:
* *isAlphaNumeric()*
it's alphanumeric
* *isAlpha()*
it's alphabetic
* *isAscii()*
it's ASCII
* *isWhitespace()*
it's whitespace
* *isControl()*
it's a control character
* *isDigit()*
it's a numeric digit
* *isGraph()*
it's a printable character that's not whitespace
* *isLowerCase()*
it's lower case
* *isPrintable()*
it's printable
* *isPunct()*
it's punctuation
* *isSpace()*
it's a space character
* *isUpperCase()*
it's upper case
* *isHexadecimalDigit()*
it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)
### Hardware Required
* Arduino or Genuino Board
### Circuit
There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.
[![](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png)](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png)
image developed using [Fritzing](http://www.fritzing.org/). For more circuit examples, see the [Fritzing project page](http://fritzing.org/projects/)
### Code
Open the serial monitor window of the Arduino Software (IDE) and type in a single character at a time, then press Send to get a report about that specific character.
/*
Character analysis operators
Examples using the character analysis operators.
Send any byte and the sketch will tell you about it.
created 29 Nov 2010
modified 2 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/CharacterAnalysis
*/
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
Serial.println("send any byte and I'll tell you everything I can about it");
Serial.println();
}
void loop() {
// get any incoming bytes:
if (Serial.available() > 0) {
int thisChar = Serial.read();
// say what was sent:
Serial.print("You sent me: \'");
Serial.write(thisChar);
Serial.print("\' ASCII Value: ");
Serial.println(thisChar);
// analyze what was sent:
if (isAlphaNumeric(thisChar)) {
Serial.println("it's alphanumeric");
}
if (isAlpha(thisChar)) {
Serial.println("it's alphabetic");
}
if (isAscii(thisChar)) {
Serial.println("it's ASCII");
}
if (isWhitespace(thisChar)) {
Serial.println("it's whitespace");
}
if (isControl(thisChar)) {
Serial.println("it's a control character");
}
if (isDigit(thisChar)) {
Serial.println("it's a numeric digit");
}
if (isGraph(thisChar)) {
Serial.println("it's a printable character that's not whitespace");
}
if (isLowerCase(thisChar)) {
Serial.println("it's lower case");
}
if (isPrintable(thisChar)) {
Serial.println("it's printable");
}
if (isPunct(thisChar)) {
Serial.println("it's punctuation");
}
if (isSpace(thisChar)) {
Serial.println("it's a space character");
}
if (isUpperCase(thisChar)) {
Serial.println("it's upper case");
}
if (isHexadecimalDigit(thisChar)) {
Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");
}
// add some space and ask for another byte:
Serial.println();
Serial.println("Give me another byte:");
Serial.println();
}
}
[[Get Code]](https://www.arduino.cc/en/Tutorial/CharacterAnalysis?action=sourceblock&num=1)
### See Also
* [String object](https://www.arduino.cc/en/Reference/StringObject) – Your Reference for String objects
* [StringAdditionOperator](https://www.arduino.cc/en/Tutorial/StringAdditionOperator) - Add strings together in a variety of ways.
* [StringAppendOperator](https://www.arduino.cc/en/Tutorial/StringAppendOperator) - Use the += operator and the concat() method to append things to Strings
* [StringCaseChanges](https://www.arduino.cc/en/Tutorial/StringCaseChanges) - Change the case of a string.
* [StringCharacters](https://www.arduino.cc/en/Tutorial/StringCharacters) - Get/set the value of a specific character in a string.
* [StringComparisonOperators](https://www.arduino.cc/en/Tutorial/StringComparisonOperators) - Get/set the value of a specific character in a string.
* [StringConstructors](https://www.arduino.cc/en/Tutorial/StringConstructors) - Initialize string objects.
* [StringIndexOf](https://www.arduino.cc/en/Tutorial/StringIndexOf) - Look for the first/last instance of a character in a string.
* [StringLength](https://www.arduino.cc/en/Tutorial/StringLength) - Get and trim the length of a string.
* [StringLengthTrim](https://www.arduino.cc/en/Tutorial/StringLengthTrim) - Get and trim the length of a string.
* [StringReplace](https://www.arduino.cc/en/Tutorial/StringReplace) - Replace individual characters in a string.
* [StringStartsWithEndsWith](https://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith) - Check which characters/substrings a given string starts or ends with.
* [StringSubstring](https://www.arduino.cc/en/Tutorial/StringSubstring) - Look for "phrases" within a given string.
* [StringToInt](https://www.arduino.cc/en/Tutorial/StringToInt) - Allows you to convert a String to an integer number.
- 说明
- 系统示例文件目录结构及说明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (与特定硬件相关,暂无)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暂无)
- ArduinoISP