Jump to content

[1.8] Copying Characters From a String


CritiqualError

Recommended Posts

[i am using Eclipse and Forge 1.8]

 

I was wondering how to take certain characters from a string.

Example:

I convert the number 123456 to a string then I want to assign every 2 characters to a variable: a="12" b="34" c="56".

Like that. How would I go about doing that?

 

Or maybe have it start from the end, so it can be like, 12345678 and end up like: a="1234" b="56" c="78". only splitting them between those 3 groups, the a group allowed to overflow as much as it wants.

 

Link to comment
Share on other sites

Thanks a bunch. It is very nice to know that I can rely on the forums when I'm stuck on a problem. Such a swift response.

I also now know new coding (i coded a bit in other programs so I kinda get how coding works, just this minecraft stuff is rather new to me) I will put this to good use and see what I can do.

 

First let's create a splitter (from the Guava library) with the characteristics we want:

static Splitter splitter = Splitter.fixedLength(2).limit(3);

 

For your first problem:

 

List<String> split = splitter.splitToList("12345678"); // returns [12, 34, 5678]

 

For the 2nd one:

List<String> split = Lists.reverse(Lists.newArrayList(splitter.split(StringUtils.reverse("12345678")))); // StringUtils is from Apache Commons Lang
split.replaceAll(StringUtils::reverse); // returns [1234, 56, 78]

 

Both libraries mentioned above are shipped with Minecraft.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.