Java C# Reference

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

The following rather meaningless program illustrates various of the string and character manipulation methods that are available in Java and which will be found to be useful in developing translators. The methods are closely related to those illustrated in C# later on this page, but they are not always completely equivalent. import java.util.*;

class Demo {

public static void main(String[] args) {

char c, c1, c2;

boolean b, b1, b2;

String s, s1, s2;

int i, i1, i2;

b = Character.isLetter(c); // true if letter

b = Character.isDigit(c); // true if digit

b = Character.isLetterOrDigit(c); // true if letter or digit

b = Character.isWhitespace(c); // true if white space

b = Character.isLowerCase(c); // true if lowercase

b = Character.isUpperCase(c); // true if uppercase

c = Character.toLowerCase(c); // equivalent lowercase

c = Character.toUpperCase(c); // equivalent uppercase

s = Character.toString(c); // convert to string

i = s.length(); // length of string

b = s.equals(s1); // true if s == s1

b = s.equalsIgnoreCase(s1); // true if s == s1, case irrelevant

i = s1.compareTo(s2); // i = -1, 0, 1 if s1 < = > s2

s = s.trim(); // remove leading/trailing whitespace

s = s.toUpperCase(); // equivalent uppercase string

s = s.toLowerCase(); // equivalent lowercase string

char[] ca = s.toCharArray(); // create character array

s = s1.concat(s2); // s1 + s2

s = s.substring(i1); // substring starting at s[i1]

s = s.substring(i1, i2); // substring s[i1 ... i2-1]

s = s.replace(c1, c2); // replace all c1 by c2

c = s.charAt(i); // extract i-th character of s

// s[i] = c; // not allowed

i = s.indexOf(c); // position of c in s[0 ...

i = s.indexOf(c, i1); // position of c in s[i1 ...

i = s.indexOf(s1); // position of s1 in s[0 ...

i = s.indexOf(s1, i1); // position of s1 in s[i1 ...

i = s.lastIndexOf(c); // last position of c in s

i = s.lastIndexOf(c, i1); // last position of c in s, <= i1

i = s.lastIndexOf(s1); // last position of s1 in s

i = s.lastIndexOf(s1, i1); // last position of s1 in s, <= i1

i = Integer.parseInt(s); // convert string to integer

i = Integer.parseInt(s, i1); // convert string to integer, base i1

s = Integer.toString(i); // convert integer to string

StringBuffer // build strings

sb = new StringBuffer(), //

sb1 = new StringBuffer("original"); //

sb.append(c); // append c to end of sb

sb.append(s); // append s to end of sb

sb.insert(i, c); // insert c in position i

sb.insert(i, s); // insert s in position i

b = sb.equals(sb1); // true if sb == sb1

i = sb.length(); // length of sb

i = sb.indexOf(s1); // position of s1 in sb

sb.delete(i1, i2); // remove sb[i1 .. i2]

sb.replace(i1, i2, s1); // replace sb[i1 .. i2] by s1

s = sb.toString(); // convert sb to real string

c = sb.charAt(i); // extract sb[i]

sb.setCharAt(i, c); // sb[i] = c

StringTokenizer // tokenize strings

st = new StringTokenizer(s, ".,"); // delimiters are . and ,

st = new StringTokenizer(s, ".,", true); // delimiters are also tokens

while (st.hasMoreTokens()) // process successive tokens

process(st.nextToken());

}

}

The following rather meaningless program illustrates various of the string and character manipulation methods that are available in C# and which will be found to be useful in developing translators. The methods are closely related to those illustrated in Java earlier on this page, but they are not always completely equivalent. using System.Text; // for StringBuilder

using System; // for Char

class demo {

public static void Main(string[] args) {

char c, c1, c2;

bool b, b1, b2;

string s, s1, s2;

int i, i1, i2;

b = Char.IsLetter(c); // true if letter

b = Char.IsDigit(c); // true if digit

b = Char.IsLetterOrDigit(c); // true if letter or digit

b = Char.IsWhiteSpace(c); // true if white space

b = Char.IsLower(c); // true if lowercase

b = Char.IsUpper(c); // true if uppercase

c = Char.ToLower(c); // equivalent lowercase

c = Char.ToUpper(c); // equivalent uppercase

s = c.ToString(); // convert to string

i = s.Length; // length of string

b = s.Equals(s1); // true if s == s1

b = String.Equals(s1, s2); // true if s1 == s2

i = String.Compare(s1, s2); // i = -1, 0, 1 if s1 < = > s2

i = String.Compare(s1, s2, true); // i = -1, 0, 1 if s1 < = > s2, ignoring case

s = s.Trim(); // remove leading/trailing whitespace

s = s.ToUpper(); // equivalent uppercase string

s = s.ToLower(); // equivalent lowercase string

char[] ca = s.ToCharArray(); // create character array

s = String.Concat(s1, s2); // s1 + s2

s = s.Substring(i1); // substring starting at s[i1]

s = s.Substring(i1, i2); // substring s[i1 ... i1+i2]

s = s.Remove(i1, i2); // remove i2 chars from s[i1]

s = s.Replace(c1, c2); // replace all c1 by c2

s = s.Replace(s1, s2); // replace all s1 by s2

c = s[i]; // extract i-th character of s

// s[i] = c; // not allowed

i = s.IndexOf(c); // position of c in s[0 ...

i = s.IndexOf(c, i1); // position of c in s[i1 ...

i = s.IndexOf(s1); // position of s1 in s[0 ...

i = s.IndexOf(s1, i1); // position of s1 in s[i1 ...

i = s.LastIndexOf(c); // last position of c in s

i = s.LastIndexOf(c, i1); // last position of c in s, <= i1

i = s.LastIndexOf(s1); // last position of s1 in s

i = s.LastIndexOf(s1, i1); // last position of s1 in s, <= i1

i = Convert.ToInt32(s); // convert string to integer

i = Convert.ToInt32(s, i1); // convert string to integer, base i1

s = Convert.ToString(i); // convert integer to string

StringBuilder // build strings

sb = new StringBuilder(), //

sb1 = new StringBuilder("original"); //

sb.Append(c); // append c to end of sb

sb.Append(s); // append s to end of sb

sb.Insert(i, c); // insert c in position i

sb.Insert(i, s); // insert s in position i

b = sb.Equals(sb1); // true if sb == sb1

i = sb.Length; // length of sb

sb.Remove(i1, i2); // remove i2 chars from sb[i1]

sb.Replace(c1, c2); // replace all c1 by c2

sb.Replace(s1, s2); // replace all s1 by s2

s = sb.ToString(); // convert sb to real string

c = sb[i]; // extract sb[i]

sb[i] = c; // sb[i] = c

string[] // tokenize strings

tokens = s.Split('.', ';'); // delimiters are . and ;

for (i = 0; i < tokens.Length; i++) // process successive tokens

Process(tokens[i]);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航