Topic: JAVA的一个小程序!如何写(只显示字符串中的数字)

  Print this page

1.JAVA的一个小程序!如何写(只显示字符串中的数字) Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-07-02 09:37

用JAVA编写个程序
只显示字符串中的 数字
比如 fd9df6fd3fd 中的 数字963
高手帮帮

2.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: why
Posted on: 2006-07-02 12:07


  public static String getDigit(String s) {
    if (s == null) { return null; }

    int len = s.length();
    if (len == 0) { return ""; }
    
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < len; i++) {
      char c = s.charAt(i);
      //could use Character.isDigit(char)
      if (c >= '0' && c <= '9') {
        sb.append(c);
      }
    }
    return sb.toString();
  }

3.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-07-02 13:07

谢谢了
3Q

4.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: why] Copy to clipboard
Posted by: Freax
Posted on: 2006-07-03 08:55

why wrote:

  public static String getDigit(String s) {
    if (s == null) { return null; }

    int len = s.length();
    if (len == 0) { return ""; }
     //could use s.toCharArray()
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < len; i++) {
      char c = s.charAt(i);
      //could use Character.isDigit(char)
      if (c >= '0' && c <= '9') {
        sb.append(c);
      }
    }
    return sb.toString();
  }


5.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: Freax] Copy to clipboard
Posted by: why
Posted on: 2006-07-03 10:49

toCharArray() is indeed more efficient.

i.e.
//could use s.toCharArray()
char[] ac = s.toCharArray();
...
// char c = s.charAt(i);
char c = ac[i];

6.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: athlon
Posted on: 2006-07-03 17:04

试试这个

String s ="fd9df6fd3fd";
System.out.println(s.replaceAll("[a-z,A-Z]*",""));

7.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: smartmen
Posted on: 2006-07-06 15:28

一滴水见太阳,好解!

8.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-07-06 21:59

谢谢各位了

9.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: why] Copy to clipboard
Posted by: sunjavaduke
Posted on: 2006-07-08 22:31

why wrote:

  public static String getDigit(String s) {
    if (s == null) { return null; }

    int len = s.length();
    if (len == 0) { return ""; }
    
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < len; i++) {
      char c = s.charAt(i);
      //could use Character.isDigit(char)
      if (c >= '0' && c <= '9') {
        sb.append(c);
      }
    }
    return sb.toString();
  }



I think Character.isDigit(char) is better.

10.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: athlon] Copy to clipboard
Posted by: sunjavaduke
Posted on: 2006-07-08 22:35

athlon wrote:
试试这个

String s ="fd9df6fd3fd";
System.out.println(s.replaceAll("[a-z,A-Z]*",""));


This is another good method~
so,I think we should learn as much as we can,especially the java API.

replaceAll
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

Pattern.compile(regex).matcher(str).replaceAll(repl)

Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid

11.Re:JAVA的一个小程序!如何写(只显示字符串中的数字) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: sunjavaduke
Posted on: 2006-07-08 22:38


Summary of regular-expression constructs
Construct Matches

Characters
x The character x
\\ The backslash character
\0n The character with octal value 0n (0 <= n <= 7)
\0nn The character with octal value 0nn (0 <= n <= 7)
\0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
\xhh The character with hexadecimal value 0xhh
\uhhhh The character with hexadecimal value 0xhhhh
\t The tab character ('\u0009')
\n The newline (line feed) character ('\u000A')
\r The carriage-return character ('\u000D')
\f The form-feed character ('\u000C')
\a The alert (bell) character ('\u0007')
\e The escape character ('\u001B')
\cx The control character corresponding to x

Character classes
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)

Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]

POSIX character classes (US-ASCII only)
\p{Lower} A lower-case alphabetic character: [a-z]
\p{Upper} An upper-case alphabetic character:[A-Z]
\p{ASCII} All ASCII:[\x00-\x7F]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]
\p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
\p{Graph} A visible character: [\p{Alnum}\p{Punct}]
\p{Print} A printable character: [\p{Graph}\x20]
\p{Blank} A space or a tab: [ \t]
\p{Cntrl} A control character: [\x00-\x1F\x7F]
\p{XDigit} A hexadecimal digit: [0-9a-fA-F]
\p{Space} A whitespace character: [ \t\n\x0B\f\r]


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923