當前位置:編程學習大全網 - 編程語言 - 字符串編程

字符串編程

//JAVA原始字符串比較方法

/**

*按字典順序比較兩個字符串。

*比較基於中每個字符的Unicode值

*琴弦。由此表示的字符序列

* & lt代碼& gt字符串& lt/code & gt;對象與

*由參數字符串表示的字符序列。結果是

*壹個負整數,如果這& lt代碼& gt字符串& lt/code & gt;目標

*按字典順序位於參數字符串之前。結果是壹個

*正整數if this & lt代碼& gt字符串& lt/code & gt;對象按字典順序排列

*跟在參數字符串後面。如果字符串

*相等;& lt代碼& gtcompareTo & lt/code & gt;退貨& lt代碼& gt0 & lt/code & gt;確切時間

*方法{@link #equals(Object)}將返回& lt代碼& gttrue & lt/code & gt;。

* & ltp & gt

*這是字典順序的定義。如果兩個字符串是

*不同,那麽要麽它們在某個索引處有不同的字符

*這是兩個字符串的有效索引,或者它們的長度不同,

*或者兩者都有。如果它們在壹個或多個索引處具有不同的字符

*位置,let & lt我& gtk & lt/I & gt;是最小的這種指數;然後是繩子

*位置& lt我& gtk & lt/I & gt;具有較小的值,因為

*通過使用& lt運算符,按字典順序位於

*其他字符串。在這種情況下,& lt代碼& gtcompareTo & lt/code & gt;返回

*位置& lt代碼& gtk & lt/code & gt;在

*兩個字符串-即值:

* & ltblockquote & gt& ltpre & gt

* this . charat(k)-another string . charat(k)

* & lt/pre & gt;& lt/block quote & gt;

*如果不存在它們不同的索引位置,則較短的

*字符串按字典順序位於較長的字符串之前。在這種情況下,

* & lt代碼& gtcompareTo & lt/code & gt;返回的長度之差

*字符串-即值:

* & ltblockquote & gt& ltpre & gt

* this . length()-another string . length()

* & lt/pre & gt;& lt/block quote & gt;

*

* @ param anotherString the & lt代碼& gt字符串& lt/code & gt;待比較。

* @返回值& lt代碼& gt0 & lt/code & gt;如果參數字符串等於

*此字符串;小於& lt代碼& gt0 & lt/code & gt;如果這個字符串

*在字典順序上小於字符串參數;和壹個

*值大於& lt代碼& gt0 & lt/code & gt;如果這個字符串是

*按字典順序大於字符串參數。

*/

public int compare to(String another String){

int len 1 = count;

int len 2 = another string . count;

int n = Math.min(len1,len 2);

char v 1[]= this . tochararray();;

char v2[]= another string . tochararray();

int i = offset

int j = anotherString.offset

if (i == j) {

int k = I;

int lim = n+I;

while(k & lt;lim) {

char c 1 = v 1[k];

char C2 = v2[k];

if (c1!= c2) {

返回c 1-C2;

}

k++;

}

}否則{

而(n -!= 0) {

char c 1 = v 1[i++];

char C2 = v2[j++];

if (c1!= c2) {

返回c 1-C2;

}

}

}

返回len 1-len 2;

}

  • 上一篇:UG 破面該如何檢查,從哪能看出破面的地方?
  • 下一篇:linux資源是怎樣管理的? 要詳細點的。。。。。。。。快!!!!
  • copyright 2024編程學習大全網