java中截取字串方法

來源:互聯網
上載者:User

標籤:ng2   com   using   index   table   str   sys   out   tar   

Java簡介substringpublic String substring(int beginIndex)返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。例如:"unhappy".substring(2) returns "happy""Harbison".substring(3) returns "bison""emptiness".substring(9) returns "" (an empty string)參數:beginIndex - 開始處的索引(包括)。返回:指定的子字串。拋出:IndexOutOfBoundsException - 如果 beginIndex 為負或大於此 String 對象的長度。--------------------------------------------------------------------------------substringpublic String substring(int beginIndex, int endIndex)返回一個新字串,它是此字串的一個子字串。該子字串從指定的 beginIndex 處開始, endIndex:到指定的 endIndex-1處結束。樣本:"hamburger".substring(3,8) returns "burge""smiles".substring(0,5) returns "smile"參數:beginIndex - 開始處的索引(包括)。endindex 結尾處索引(不包括)。返回:指定的子字串。拋出:IndexOutOfBoundsException - 如果 beginIndex 為負,或length大於字串長度。樣本 var str="Hello world!"document.write(str.substring(1,3)); 上面返回字串:"el";str.substring(1,2) //返回estr.substring(1) //返回"ello world";還有此函數中會出現奇怪的現象,當出現str.substring(5,0);這又是怎麼回事,不過返回的是"hello",str.substring(5,1) //返回"ello",截去了第一位,返回餘下的.可見substring(start,end),可以有不同的說明,即start可以是要返回的長度,end是所要去掉的多少個字元(從首位開始).在JS中,substr(start,length),用得較方便.-----------------------------------------------------------------------------------------------編輯本段C#中變數.Substring(參數1,參數2);截取字串的一部分,參數1為左起始位元,參數2為截取幾位。如:string s1 = str.Substring(0,2);C#中有兩個重載函數舉例如下代碼,VS2005編譯通過using System;using System.Collections.Generic;using System.Text;namespace sln_sub{class Program{static void Main(string[] args){string myString = "A quick fox is jumping over the lazy dog";//Substring()在C#中有兩個重載函數//分別如下樣本string subString1 = myString.Substring(0);//如果傳入參數為一個長整, 且大於等於0,//則以這個長整的位置為起始,//截取之後餘下所有作為字串.//如若傳入值小於0,//系統會拋出ArgumentOutOfRange異常//表明參數範圍出界string subString2 = myString.Substring(0, 11);//如果傳入了兩個長整參數,//前一個為參數子串在原串的起始位置//後一個參數為子串的長度//如不合條件同樣出現上述異常Console.WriteLine(subString1);Console.WriteLine(subString2);Console.ReadLine();}}}程式輸出的結果:A quick fox is jumping over the lazy dogA quick fox另外,求字元a在字串A中的位置:A.Indexof(‘a‘)。編輯本段js用法在JS中, 函式宣告: stringObject.substring(start,stop)start是在原字串檢索的開始位置,stop是檢索的終止位置,返回結果中不包括stop所指字元.編輯本段CB用法用途Returns the substring at the specified location within a String object.函數用法及舉例strVariable.substring(start, end)"String Literal".substring(start, end)用法說明:返回一個字串,其中start是起始的index,end是終止的index,返回的字串包含起始index的字元,但是不包含end的字元。這個是string類下的一個method。用法執行個體
function SubstringDemo(){var ss; //Declare variables.var s = "The rain in Spain falls mainly in the plain..";ss = s.substring(12, 17); //Get substring.return(ss); //Return substring.}

java中截取字串方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.