新聞中心
如何在Android中從文件中讀寫(xiě)字符串
1、通過(guò)File獲取文件
創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、撫州網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、成都商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為撫州等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
2、打開(kāi)輸入流,讀取文件
寫(xiě)文件:
1、創(chuàng)建文件
2、打開(kāi)輸出流,寫(xiě)入文件內(nèi)容
示例:
12345678910111213
讀文件:String content = ""; //文件內(nèi)容字符串 //通過(guò)路徑/sdcard/foo.txt打開(kāi)文件 File file = new File("/sdcard/foo.txt"); try { InputStream instream = new FileInputStream(file);//讀取輸入流 InputStreamReader inputreader = new InputStreamReader(instream);//設(shè)置流讀取方式 BufferedReader buffreader = new BufferedReader(inputreader); while (( line = buffreader.readLine()) != null) { content += line + "\n";//讀取的文件內(nèi)容 } }catch(Exception ex){ }
寫(xiě)文件: File file = new File("/sdcard/foo.txt");// if(!file.exists()) file.createNewFile();//如果文件不存在,創(chuàng)建foo.txt try { OutputStream outstream = new FileOutputStream(file);//設(shè)置輸出流 OutputStreamWriter out = new OutputStreamWriter(outstream);//設(shè)置內(nèi)容輸出方式 out.write("文字內(nèi)容");//輸出內(nèi)容到文件中 out.close(); } catch (java.io.IOException e) { e.printStackTrace(); }
android 幾個(gè)經(jīng)常用到的字符串的截取
幾個(gè)經(jīng)常用到的字符串的截取
string str="123abc456";
int i=3;
1 取字符串的前i個(gè)字符
str=str.Substring(0,i); // or str=str.Remove(i,str.Length-i);
2 去掉字符串的前i個(gè)字符:
str=str.Remove(0,i); // or str=str.Substring(i);
3 從右邊開(kāi)始取i個(gè)字符:
str=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i);
4 從右邊開(kāi)始去掉i個(gè)字符:
str=str.Substring(0,str.Length-i); // or str=str.Remove(str.Length-i,i);
5 判斷字符串中是否有"abc" 有則去掉之
using System.Text.RegularExpressions;
string str = "123abc456";
string a="abc";
Regex r = new Regex(a);
Match m = r.Match(str);
if (m.Success)
{
//綠色部分與紫色部分取一種即可。
str=str.Replace(a,"");
Response.Write(str);
string str1,str2;
str1=str.Substring(0,m.Index);
str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);
Response.Write(str1+str2);
}
6 如果字符串中有"abc"則替換成"ABC"
str=str.Replace("abc","ABC");
************************************************
string str="adcdef"; int indexStart = str.IndexOf("d");
int endIndex =str.IndexOf("e");
string toStr = str.SubString(indexStart,endIndex-indexStart);
c#截取字符串最后一個(gè)字符的問(wèn)題!
str1.Substring(str1.LastIndexOf(",")+1)
android 怎么獲取字符串中指定的字符
Android開(kāi)發(fā)中截取某字符串或者路徑中的某字符串的方法substr(start,length)、substring(start,end)、charAt(int index)、indexOf(int str,int fromIndex)
substr(start,length) :substr是從起始點(diǎn)截取某個(gè)長(zhǎng)度的字符串
substring(start,end):substring是截取2個(gè)位置之間及start-end之間的字符串
charAt(int index):實(shí)現(xiàn)從字符串中提取指定位置的字符
indexOf(int str,int fromIndex):返回指定字符在此字符串中第一次出現(xiàn)處的索引。如果在此 String 對(duì)象表示的字符序列中出現(xiàn)值為 str 的字符,則返回第一次出現(xiàn)該字符的索引(以 Unicode 代碼單元表示
如何得到android EditText里面的字符串
Android中有許多寫(xiě)法創(chuàng)建事件處理方式,一般會(huì)使用Android:onClick屬性來(lái)指定。
舉例說(shuō)明:
實(shí)現(xiàn)攝氏溫度到華氏溫度的轉(zhuǎn)變
1、
EditText editText1 =(EditText) findViewById (R.id.editText1)
c=Integer.parseInt(editText1.getText().toString());
用來(lái)獲取editText1中的信息
2、
EditText editText2 =(EditText) findViewById (R.id.editText2);
f=(9.0*c)/5.0+32.0;
editText2.setText(String.valueOf(f));
通過(guò)editText1 獲取的信息然后經(jīng)過(guò)計(jì)算
將計(jì)算的結(jié)果返回editText2中然后在editText2中顯示出來(lái)
擴(kuò)展資料:
EditText 控件的用法
EditText 在開(kāi)發(fā)中也是經(jīng)常用到的控件,也是一個(gè)比較必要的組件。
它是用戶跟Android應(yīng)用進(jìn)行數(shù)據(jù)傳輸?shù)拇皯簟?/p>
1、android:text設(shè)置文本內(nèi)容。?
2、android:textColor字體顏色。?
3、android:hint內(nèi)容為空時(shí)候顯示的文本。?
4、android:textColorHint為空時(shí)顯示的文本的顏色。?
5、android:maxLength限制顯示的文本長(zhǎng)度,超出部分不顯示。?
6、android:minLines設(shè)置文本的最小行數(shù)。?
7、android:gravity設(shè)置文本位置,如設(shè)置成“center”,文本將居中顯示。?
8、android:drawableLeft在text的左邊輸出一個(gè)drawable,如圖片。?
網(wǎng)頁(yè)題目:android字符串,Android字符串截?cái)鄼z測(cè)
URL分享:http://fisionsoft.com.cn/article/phpjhi.html