how to replace characters in a string with integers?

Steven

Say I have the the string: " aab + bab = b " and I want to

  • replace all the a characters with the integer 0,
  • replace all the b characters with the integer 1

so 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);
Reimeus

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

php string replace with escape characters

분류에서Dev

Replace integers in a string using Visual Basic

분류에서Dev

Objective-C: Randomly replace characters in string

분류에서Dev

replace a line that contains a string with special characters

분류에서Dev

Replace a random block of characters in a string in R

분류에서Dev

How to replace strings in a string

분류에서Dev

How to replace all special characters using javascript

분류에서Dev

How can i replace particular characters in a file

분류에서Dev

Storing characters and integers

분류에서Dev

Perl regex: how to replace terminal control characters with html tags?

분류에서Dev

How should i replace double space(continuous space) by double characters?

분류에서Dev

How to replace formatting of "0 or more characters" (*) wildcard in MS Word?

분류에서Dev

How can I use sed to replace multiple characters?

분류에서Dev

Common-Lisp: How to sort the characters of a string?

분류에서Dev

How to remove duplicate characters in a string using regex?

분류에서Dev

How to limit string characters when rendered in MeteorJS

분류에서Dev

How to 'drop'/delete characters from in front of a string?

분류에서Dev

How to count characters or unique string in matlab

분류에서Dev

python how to uppercase some characters in string

분류에서Dev

I am confuse how to replace string preg replace

분류에서Dev

How do I replace a set of single characters and white space with the same characters but no white space

분류에서Dev

Display a list of integers as a String

분류에서Dev

How to sed and replace string with a folder path

분류에서Dev

how to replace number in a string that contains brackets in php

분류에서Dev

how to replace @ string and capitalize letters with sed

분류에서Dev

How to replace a string according to their length with "*" (astrik) letter

분류에서Dev

python how to replace string by regex group?

분류에서Dev

How to use string in JS replace() parameter

분류에서Dev

Replace from characters with dots

Related 관련 기사

  1. 1

    php string replace with escape characters

  2. 2

    Replace integers in a string using Visual Basic

  3. 3

    Objective-C: Randomly replace characters in string

  4. 4

    replace a line that contains a string with special characters

  5. 5

    Replace a random block of characters in a string in R

  6. 6

    How to replace strings in a string

  7. 7

    How to replace all special characters using javascript

  8. 8

    How can i replace particular characters in a file

  9. 9

    Storing characters and integers

  10. 10

    Perl regex: how to replace terminal control characters with html tags?

  11. 11

    How should i replace double space(continuous space) by double characters?

  12. 12

    How to replace formatting of "0 or more characters" (*) wildcard in MS Word?

  13. 13

    How can I use sed to replace multiple characters?

  14. 14

    Common-Lisp: How to sort the characters of a string?

  15. 15

    How to remove duplicate characters in a string using regex?

  16. 16

    How to limit string characters when rendered in MeteorJS

  17. 17

    How to 'drop'/delete characters from in front of a string?

  18. 18

    How to count characters or unique string in matlab

  19. 19

    python how to uppercase some characters in string

  20. 20

    I am confuse how to replace string preg replace

  21. 21

    How do I replace a set of single characters and white space with the same characters but no white space

  22. 22

    Display a list of integers as a String

  23. 23

    How to sed and replace string with a folder path

  24. 24

    how to replace number in a string that contains brackets in php

  25. 25

    how to replace @ string and capitalize letters with sed

  26. 26

    How to replace a string according to their length with "*" (astrik) letter

  27. 27

    python how to replace string by regex group?

  28. 28

    How to use string in JS replace() parameter

  29. 29

    Replace from characters with dots

뜨겁다태그

보관