Jump to content

convert String of chars with spaces to BigInteger

- - - - -

  • Please log in to reply
8 replies to this topic

#1
error9900

error9900

    Newbie

  • Members
  • PipPip
  • 10 posts
I'm trying to convert a String of characters that has spaces in it to a BigInteger...and I'm failing. Any help would be greatly appreciated.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Examples please. And your code.

#3
error9900

error9900

    Newbie

  • Members
  • PipPip
  • 10 posts
Well, is there a function to remove all spaces from a String? I believe trim() only removes spaces from the beginning and end of the String.

Or am I gonna have to right a loop or something to do it?

#4
error9900

error9900

    Newbie

  • Members
  • PipPip
  • 10 posts
Also, I guess I need to get the characters in the String into digits. Is getBytes going to be the best way to do this?

EDIT: Here's the String:

String message = "The quick brown fox jumps over the lazy dog.";

I'm not necessarily looking for the code. Even if someone could just let me know which functions or type of code (e.g. loop, etc.) would be the best way to do this, I could take it from there.

#5
error9900

error9900

    Newbie

  • Members
  • PipPip
  • 10 posts
Alright. I actually just ended up using String.getBytes() to convert it into bytes, and then was able to declare a BigInteger with the value from the bytes.

#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
anyway to remove all spaces you can use String.replaceAll()

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
An example that removes all whitespaces from the string:

        String s = "The quick brown fox jumps over the lazy dog.";
        s = s.replaceAll("\\s", "");
        System.out.println(s); 
Results: Thequickbrownfoxjumpsoverthelazydog.

s.replaceAll() takes regex as an argument, so if you are not familiar with them, you should look them up.

#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
this should work as well:

s = s.replaceAll(" ", "");


#9
error9900

error9900

    Newbie

  • Members
  • PipPip
  • 10 posts
Thanks for the tip!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users