例如:字符串“12+3-4*5”经过程序处理以后为字符数组:
{“12”, “+”, “3”, “-“, “4”, “*”, “5”}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
String input="12+3";
String regExp="\\D";//非数字的正则表达式
String [] nums=input.split(regExp);//取得数字
System.out.println(Arrays.asList(nums));
//取得非数字
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group());
}
public List<String> splitExpression(String str) {
List<String> list = new ArrayList<>();
//start与end记录从源字符串中截取的开始位置与结束位置
int start = 0, end = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
end = i;
//截取数字
if (end > start) {
list.add(str.substring(start, end));
}
//截取运算符
list.add(str.substring(end, end + 1));
start = i + 1;
}
}
//截取最后的数字
if (start >= end) {
list.add(str.substring(start));
}
return list;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static int get_parts(const char* txt,int txtlen,char resdata[][10])
{
int x=0,y=0;
for(x=0;x<txtlen;x++)
{
int i=0;
if((txt[x]>='0')&&(txt[x]<='9'))
{
while((txt[x]>='0')&&(txt[x]<='9'))
{
resdata[y][i++]=txt[x];
x++;
}
resdata[y][i++]=0;
y++;
}
if((txt[x]=='+')||(txt[x]=='-')||(txt[x]=='*'))
{
resdata[y][0]=txt[x];
resdata[y][1]=0;
y++;
}
}
return y;
}
int main()
{
char* str="12+3-4*5";
char res[10][10];
int parts_count=0;
int i=0;
parts_count = get_parts(str,strlen(str),res);
for(i=0;i<parts_count;i++)
{
printf("%s \t",res[i]);
}
return 0;
}