ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

javaÖÐstringÀà³£Ó÷½·¨
2014-11-24 00:18:05 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:7210´Î
Tags£ºjava string ³£Óà ·½·¨

¸ø³ö×Ö·û´®µÄÁ½¸öλÖã¬pos1ºÍpos2, ÈçºÎÌæ»»pos1ºÍpos2Ö®¼äµÄ×Ö´®
´ð£ºÓÃsubString £¬ String.substring(0,pos1)+newString+String.substring(pos2+1)
Áí¸½java×Ö·û´®²Ù×÷·½·¨£º
ÔÚJAVAÓïÑÔÖУ¬×Ö·û´®Êý¾Ýʵ¼ÊÉÏÓÉStringÀàËùʵÏֵġ£Java×Ö·û´®Àà·ÖΪÁ½ÀࣺһÀàÊÇÔÚ³ÌÐòÖв»»á±»¸Ä±ä³¤¶ÈµÄ²»±ä×Ö·û´®£»¶þÀàÊÇÔÚ³ÌÐòÖлᱻ¸Ä±ä³¤¶ÈµÄ¿É±ä×Ö·û´®¡£Java»·¾³ÎªÁË´æ´¢ºÍά»¤ÕâÁ½Àà×Ö·û´®ÌṩÁË StringºÍStringBufferÁ½¸öÀà¡£
Ò»¡¢´´½¨×Ö·û´®
Àý£º String str=new£¨¡±This is a String¡±);


»òÕß String str=¡±This is a String¡±;


¶þ¡¢µÃµ½×Ö·û´®¶ÔÏóµÄÓйØÐÅÏ¢
1.ͨ¹ýµ÷ÓÃlength()·½·¨µÃµ½StringµÄ³¤¶È.
Àý£º


String str=¡±This is a String¡±;
int len =str.length();


2.StringBufferÀàµÄcapacity()·½·¨ÓëStringÀàµÄ length()µÄ·½·¨ÀàËÆ£¬µ«ÊÇËý²âÊÔÊÇ·ÖÅä¸øStringBufferµÄÄÚ´æ¿Õ¼äµÄ´óС£¬¶ø²»Êǵ±Ç°±»Ê¹ÓÃÁ˵ÄÄÚ´æ¿Õ¼ä¡£
3£®Èç¹ûÏëÈ·¶¨×Ö·û´®ÖÐÖ¸¶¨×Ö·û»ò×Ó×Ö·û´®ÔÚ¸ø¶¨×Ö·û´®µÄλÖ㬿ÉÒÔÓà indexOf()ºÍlastIndexOf()·½·¨¡£


String str=¡±This is a String¡±;
Int index1 =str.indexOf(¡°i¡±);   //index=2
Int index2=str.indexOf(¡®i¡¯,index+1);   //index2=5
Int index3=str.lastIndexOf(¡°I¡±);   //index3=15
Int index4=str.indexOf(¡°String¡±);  //index4=10


Èý¡¢String ¶ÔÏóµÄ±È½ÏºÍ²Ù×÷
1£®String ¶ÔÏóµÄ±È½Ï
StringÀàµÄequals()·½·¨ÓÃÀ´È·¶¨Á½¸ö×Ö·û´®ÊÇ·ñÏàµÈ¡£


String str=¡±This is a String¡±;
Boolean result=str.equals(¡°This is another String ¡°);
//result=false


2£®String¶ÔÏóµÄ·ÃÎÊ
A¡¢·½·¨charAt()ÓÃÒԵõ½Ö¸¶¨Î»ÖõÄ×Ö·û¡£


String str=¡±This is a String¡±;
char chr=str.charAt(3); //chr=¡±i¡±


B¡¢·½·¨getChars()ÓÃÒԵõ½×Ö·û´®µÄÒ»²¿·Ö×Ö·û´®


public void getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin)
String str=¡±This is a String¡±;
Char chr =new char[10];
Str.getChars(5,12,chr,0);  //chr=¡±is a St¡±


C¡¢subString()ÊÇÌáÈ¡×Ö·û´®µÄÁíÒ»ÖÖ·½·¨£¬Ëü¿ÉÒÔÖ¸¶¨´ÓºÎ´¦¿ªÊ¼ÌáÈ¡×Ö·û´®ÒÔ¼°ºÎ´¦½áÊø¡£
3£®²Ù×÷×Ö·û´®
A¡¢replace()·½·¨¿ÉÒÔ½«×Ö·û´®ÖеÄÒ»¸ö×Ö·ûÌ滻ΪÁíÒ»¸ö×Ö·û¡£


String str=¡±This is a String¡±;
String str1=str.replace(¡®T¡¯,'t¡¯); //str1=¡±this is a String¡±


B¡¢concat()·½·¨¿ÉÒÔ°ÑÁ½¸ö×Ö·û´®ºÏ²¢ÎªÒ»¸ö×Ö·û´®¡£


String str=¡±This is a String¡±;
String str1=str.concat(¡°Test¡±); //str1=¡±This is a String Test¡±


C¡¢toUpperCase()ºÍtoLowerCase()·½·¨·Ö±ðʵÏÖ×Ö·û´®´óСдµÄת»»¡£


String str=¡±THIS IS A STRING¡±;
String str1=str.toLowerCase(); //str1=¡±this is a string¡±;


D¡¢trim()·½·¨¿ÉÒÔ½«×Ö·û´®ÖпªÍ·ºÍ½áβ´¦µÄ¿Õ¸ñÈ¥µô.


String str=¡±This is a String   ¡°;
String str1=str.trim();   // str1=¡±This is a String¡±


E¡¢String ÀàÌṩ¾²Ì¬·½·¨valueOf(),Ëü¿ÉÒÔ½«ÈκÎÀàÐ͵ÄÊý¾Ý¶ÔÏóת»»ÎªÒ»¸ö×Ö·û´®¡£Èç


System.out.println(String,ValueOf(math,PI));


ËÄ¡¢Ð޸Ŀɱä×Ö·û´®
StringBufferÀàΪ¿É±ä×Ö·û´®µÄÐÞ¸ÄÌṩÁË3ÖÖ·½·¨£¬ÔÚ×Ö·û´®Öмä²åÈëºÍ¸Ä±äij¸öλÖÃËùÔÚµÄ×Ö·û¡£
1£®ÔÚ×Ö·û´®ºóÃæ×·¼Ó£ºÓÃappend()·½·¨½«¸÷ÖÖ¶ÔÏó¼ÓÈëµ½×Ö·û´®ÖС£
2£®ÔÚ×Ö·û´®Öмä²åÈ룺ÓÃinsert()·½·¨¡£Àý


StringBuffer str=new StringBuffer(¡°This is a String¡±);
Str.insert(9,¡±test¡±);
System.out.println(str.toString());


Õâ¶Î´úÂëÊä³öΪ£ºThis is a test String
3.¸Ä±äij¸öλÖÃËùÔÚµÄ×Ö·û£¬ÓÃsetCharAt()·½·¨¡£
Str.setCharAt(2,¡±k¡±);
Õâ¸öº¯ÊýÔ­ÐÎsb.setCharAt(int,char),×¢ÒâµÚ¶þ¸ö²ÎÊýÊÇchar,ÄãÓÃË«ÒýºÅ¾Í³ÉÁË×Ö·û´®,Ó¦¸ÃʹÓõ¥ÒýºÅ,¼´sb.setCharAt(2,¡¯k');


¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºÊ²Ã´ÊÇÈí¼þÖÊÁ¿ºÍÈí¼þÖÊÁ¿±£Ö¤ (S.. ÏÂһƪ£º500Ç¿¹«Ë¾ÃæÊԵľ­µäÕýÈ·Óë´íÎó»Ø..

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿