Say I have the the string: " aab + bab = b " and I want to
a
characters with the integer 0,b
characters with the integer 1so it will become:
001 + 101 = 1
what would be the easiest way to do this?
so far I parced the equation into three parts:
System.out.println("Enter an Equation of variables");
_inString = _in.nextLine();
//find the three different parts of the equation
String _noSpaces = _inString.replaceAll("\\s+","");
String delims = "[+,=]";
String[] _tokens = _noSpaces.split(delims);
You can chain replace
methods together
String str = " aab + bab = b ";
str = str.replace("a", "0").replace("b", "1");
We require an assignment back to 'str' because str.replace() returns a new String and the original string would be unchanged. [Because String is immutable in Java]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다