java - How to extract only the letters from string equation Java
问题描述
I'm trying to extract the letters for the variables in a String "equation". Then save it to a String array called variables. However, when I print the variables array it is full of "empty containers"
How do I make it so that there are no empty spaces in the String[] variables containers?
String equation = "(a+2)/3*b-(28-c)";
String[] variables = equation.split("[+-/*0-9()]");
for(int i = 0 ; i < variables.length; ++i)
{
System.out.println(variables[i]);
}
解决方案
首先用空字符串替换所有非可变字符(匹配[+*-\\/0-9)(]
)然后将其更改为char数组,试试这个
String equation="(a+2)/3*b-(28-c)";
char[] variables= equation.replaceAll("[+*-\\/0-9)(]","").toCharArray();
for(int i = 0 ; i < variables.length; ++i)
{
System.out.println(variables[i]);
}
推荐阅读
- r - 如何将数据框转换为R中的时间序列数据
- mysql - 如果到达最后一行,则从第一行返回
- python - tensorflow.python.util.tf_export.symbolalreadyexposederror: 符号 name_scope_v1 已公开为 ()
- sql - 从空格和逗号之间的列中提取某些文本模式的方法
- vue.js - 如何在 vuejs 中创建组件并动态挂载
- visual-studio-code - 导出 WSL 上 Visual Studio Code 的扩展列表
- node.js - 将库函数转换为生成器
- sql - Sqoop 增量导入 Lastmodified 失败并出现错误:无法从数据库获取当前时间
- java - 创建三个实体时传递的分离实体持续出现错误
- extract - Tableau 从视图中提取而不是从 postgres 或 mysql 表中提取