Ⅰ 怎麼用java把輸入的兩串字元串中的數字提取出來,並且將兩串數字相乘輸出
解決方案:
使用正則表達式抽取數字子串;
使用Interger.parse將數字子串轉為整數類型;
計算兩個數字相乘即可;
Ⅱ JAVA如何從字元串中獲得數字
首先求
該字元串
度
截取除
位
字元
OK
代碼
假設String
str="100c";
int
i=str.length();//str.length
str.length()忘
自
試
int
tem=Integer.parseInt(str.subString(0,i-2));
tem
溫度咯
Ⅲ java中怎麼將字元串中的數字取出來
publicclass取數字
{
publicstaticvoidmain(String[]args)
{
System.out.println(" ==========將字元串中的數字取出來========== ");
init();
}//初始化!
privatestaticvoidinit()
{
//分割!
String[]s="今天是2017年09月01日".split("\D+");
//列印分割的!
for(inti=0;i<s.length;i++)
{
System.out.print(s[i]+"");
}
System.out.println();
}
}
Ⅳ Java如何從字元串中提取數字
使用正則表達式可以很方便地從文本中截取數字,下面是詳細代碼:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String phoneString = "哈哈,13888889999";
// 提取數字
// 1
Pattern pattern = Pattern.compile("[^0-9]");
Matcher matcher = pattern.matcher(phoneString);
String all = matcher.replaceAll("");
System.out.println("phone:" + all);
// 2
Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");
}
}
Ⅳ java如何截取字元串中的數字並計算
void calcNetIncome(String str){
int num = 0;
String temp = "";
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)=='-'||(str.charAt(i)>='0'&&str.charAt(i)<='9')){
while(i < str.length()){
temp+=str.charAt(i);
if(i+1 < str.length()&&(!(str.charAt(i+1)>='0'&&str.charAt(i+1)<='9'))){
break;
}
i++;
}
num+=Integer.parseInt(temp);
temp = "";
}
}
System.out.println(num);
}
Ⅵ java截取字元串中的數字,並且分組
用正則表達式按數字和中文的交界處切分字元串,就可以實現你的要求,完整的Java程序如下
public class F{
public static void main(String[] args){
String str="魚豆腐20海帶3掌中寶8雞翅2可樂2";
String[] s=str.split("(?<=[0-9])(?=[u4e00-u9fa5])");
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
}
}
Ⅶ java如何從任意字元串中截取數字
使用正則表達式可以很方便地從文本中截取數字,下面是詳細代碼:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String phoneString = "哈哈,13888889999";
// 提取數字
// 1
Pattern pattern = Pattern.compile("[^0-9]");
Matcher matcher = pattern.matcher(phoneString);
String all = matcher.replaceAll("");
System.out.println("phone:" + all);
// 2
Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");
}
}