分享
 
 
 

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]);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有