2.1 determine which character a char contains, 2.1char

Source: Internet
Author: User

2.1 determine which character a char contains, 2.1char
Knowledge point:

1. char. IsControl

2. char. IsPunctuation

3. char. IsSurrogate

4. char. IsWhitespace

5. char. IsDigit

6. char. IsNumber

7. char. IsSeparation

8. char. IsSymbol

 

Problem:

There is a char type variable that you want to determine whether it contains letters, digits, numbers, punctuation marks, control characters, delimiters, blank spaces, or replacement characters. Similarly, a string variable may be used to determine the character at one or more positions in the string.

Solution

To determine a char value, you can use the built-in static method of the System. Char structure, as shown below:

1. char. IsControl

2. char. IsPunctuation

3. char. IsSurrogate

4. char. IsWhitespace

5. char. IsDigit

6. char. IsNumber

7. char. IsSeparation

8. char. IsSymbol

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace _ 02 determine the characters in a Char {class Program {static void Main (string [] args) {Console. writeLine ("enter a character:"); string symbol = Console. readLine (); CharKind ck = GetCharKind (Convert. toChar (symbol); Console. writeLine (ck); string symbol1 = Console. readLine (); int position = Convert. toInt32 (Console. readLine (); CharKind ck1 = GetCharKindInString (symbol1, position); Console. writeLine (ck1); Console. readKey ();} public static CharKind GetCharKind (char theChar) {if (char. isLetter (theChar) {return CharKind. letter;} else if (char. isNumber (theChar) {return CharKind. number;} else if (char. isPunctuation (theChar) {return CharKind. punctuation;} else {return CharKind. unknown;} // determines the character type of a position in the string. public static CharKind GetCharKindInString (string theString, int CharPosition) {if (char. isLetter (theString, CharPosition) {return CharKind. letter;} else if (char. isNumber (theString, CharPosition) {return CharKind. number;} else if (char. isPunctuation (theString, CharPosition) {return CharKind. punctuation;} else {return CharKind. unknown ;}} public enum CharKind {Letter, Number, Punctuation, Unknown }}View Code

 


Verification Result 1 8 Number
Java determines whether a String contains a character char. For example, it determines whether "abcde" contains 'C '.

Print by yourself ~
String str = "abcd ";
If (str. indexOf (String. valueOf ('C') =-1 ){
// The string does not contain c
} Else {
// The String contains c, which is in the str. indexOf (String. valueOf ('C') position

If it is defined as char c = '\ 010', the number of characters in variable c is [10]

8. If it is defined as char c = '\ 010', the number of characters in variable c is [1 ].
The char type is 1 character;

9. If it is defined as: int x = 3, y = 2; float a = 2.5, B = 3.5; then the value of the following expression is [1]
(X + y) % 2 + (int) a/(int) B
(Int) a/(int) B = (int) (2/3) = 0

10. if the ASCII code of the letter a is 97 in decimal format and ch is a complex variable, the expression ch = 'A' + '8'-'3' is [f ].
Equivalent to 97 + 8-3 = 102, followed by 5 letters;

1. The putchar function can output a [D] to the terminal ].
A) integer variable expression B) Real Variable Value
C) string D) character or character variable value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.