本文目錄一覽:
JAVA中怎樣輸入字符串
1.首先,導入java.util.*包。
2.然後,你需要新建一個讀取標準輸入(鍵盤)的掃描器對象。
3.現在,你可以從鍵盤輸入字符串了。
擴展資料:
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行複雜的編程 。
Java具有簡單性、面向對象、分佈式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。
Java可以編寫桌面應用程序、Web應用程序、分佈式系統和嵌入式系統應用程序等。
參考資料:java-百度百科
java 字符串操作
char[] abc={1,2,3}表示這是一個阿斯可瑪為1,2,3的三個字符(無法正常輸出),你寫成這樣就好了:char[] abc={‘1′,’2′,’3’}
java對字符串的操作
這是你的作業吧?
先把字符串打散成 Char[] oldstrchars,
for(int i=0;i++;ioldstrchars.size())
String newstr = new String();
再用ChartoInt方法(具體方法查手冊)使字符變成數字,
進行各種判斷
根據判斷結果不一樣,操作newstr
反之同理了
JAVA字符串操作
import java.io.*;
public class MyTest2 {
public static void main(String[] args) {
MyTest2 test = new MyTest2();
test.countCharacter();
}
public void countCharacter(){
String file = “e:\\1.txt”;
String file2 = “e:\\2.txt”;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(file2));
String line = null;
int tot = 0;//用來統計數,假如這裡統計字母a的出現次數
int index = 0;
while((line=br.readLine()) != null){
for (int i = 0; i line.length(); i++) {
if(line.substring(i,(i+1)).equals(“a”)){
tot++;
}
}
char emp = ‘b’;
char emp1 = ‘x’;
String line1 = line.replaceAll(“bc”,”c”);//將”bc”替換成”c”
bw.write(line1);
bw.write(“\r\n”);
}
System.out.println(“字符a在文中出現的次數是:” + tot);
br.close();
bw.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
代碼給你了,希望你能好好看看,流的讀寫很重要,也很基礎還有幾個字符串的基本操作。東西學到手才是自己的。
java 字符串
new String(…);
是新創建一個對象。
String a = new String(“aaaaa”);
這個就是創建一個新的對象,在堆中開闢一塊新的空間。
String.valueOf(…);
這個放回的是一個 String型的實例。該方法返回的實例與他的參數具有相同的值,這種其實是一種靜態工廠方法,實際上是類型轉換的方法。
比如 String a = String.valueOf(123);
那麼a的值就是”123″;
Object.toString();
也是返回一個String的實例。
返回該對象的字符串表示。通常,toString 方法會返回一個「以文本方式表示」此對象的字符串。結果應是一個簡明但易於讀懂。建議所有子類都重寫此方法。Object 類的 toString 方法返回一個字符串,該字符串由類名(對象是該類的一個實例)、at 標記符「@」和此對象哈希碼的無符號十六進制表示組成。換句話說,該方法返回一個字符串,它的值等於:
getClass().getName() + ‘@’ + Integer.toHexString(hashCode())
求大神,java中字符串的所有方法和用法。
1、length() 字符串的長度
例:char chars[]={‘a’,’b’.’c’};
String s=new String(chars);
int len=s.length();
2、charAt() 截取一個字符
例:char ch;
ch=”abc”.charAt(1); 返回’b’
3、 getChars() 截取多個字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart指定了子串開始字符的下標,sourceEnd指定了子串結束後的下一個字符的下標。因此, 子串包含從sourceStart到sourceEnd-1的字符。接收字符的數組由target指定,target中開始複製子串的下標值是targetStart。
例:String s=”this is a demo of the getChars method.”;
char buf[]=new char[20];
s.getChars(10,14,buf,0);
4、getBytes()
替代getChars()的一種方法是將字符存儲在位元組數組中,該方法即getBytes()。
5、toCharArray()
6、equals()和equalsIgnoreCase() 比較兩個字符串
7、regionMatches() 用於比較一個字符串中特定區域與另一特定區域,它有一個重載的形式允許在比較中忽略大小寫。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8、startsWith()和endsWith()
startsWith()方法決定是否以特定字符串開始,endWith()方法決定是否以特定字符串結束,以空格區分這些字符串。
9、equals()和==
equals()方法比較字符串對象中的字符,==運算符比較兩個對象是否引用同一實例。
例:String s1=”Hello”;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比較字符串
11、indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出現的地方。
lastIndexOf() 查找字符或者子串是後一次出現的地方。
12、substring()
它有兩種形式,第一種是:String substring(int startIndex)
第二種是:String substring(int startIndex,int endIndex)
13、concat() 連接兩個字符串
14 、replace() 替換
它有兩種形式,第一種形式用一個字符在調用字符串中所有出現某個字符的地方進行替換,形式如下:
String replace(char original,char replacement)
例如:String s=”Hello”.replace(‘l’,’w’);
第二種形式是用一個字符序列替換另一個字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
15、trim() 去掉起始和結尾的空格
16、valueOf() 轉換為字符串
17、toLowerCase() 轉換為小寫
18、toUpperCase() 轉換為大寫
19、StringBuffer構造函數
StringBuffer定義了三個構造函數:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。
(2)、ensureCapacity() 設置緩衝區的大小
void ensureCapacity(int capacity)
(3)、setLength() 設置緩衝區的長度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、append() 可把任何類型數據的字符串表示連接到調用的StringBuffer對象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(“a=”).append(a).append(“!”).toString();
輸出:a=42!
(7)、insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定將字符串插入到StringBuffer對象中的位置的下標。
(8)、reverse() 顛倒StringBuffer對象中的字符
StringBuffer reverse()
(9)、delete()和deleteCharAt() 刪除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替換
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248026.html