English: How to separate the components of a word or a multi-digit number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

English: How to separate the components of a word or a multi-digit number?

En: How can Java be crushed a word or a multi-digit number and use each of its components separately as a separate variable? What kind of class or method should I use to do this? E.g.: input =1234, var1=12, var2=34 فا: چگونگی جداسازی اجزای یک کلمه یا یک عدد چند رقمی با جاوا چه طور می توان با جاوا اجزای یک کلمه یا یک عدد چند رقمی را از یکدیگر جدا کرد و از هر کدام از اجزای جداشده آن به عنوان متغیری جداگانه استفاده کرد؟ برای این کار چه کلاس یا متود هایی نیاز است؟ مثال: ورودی=۱۲۳۴، متغیر اول=۱۲، متغیر دوم=۳۴

14th Jan 2018, 3:10 PM
Amir01
Amir01 - avatar
3 Answers
+ 2
String has a method substring, which returns a specified part of the string given indices from and to String str="1234" String s1=str.substring(0, 2) String s2=str.substring(2,4) s1 will be "12" s2 will be "34"
14th Jan 2018, 3:25 PM
michal
+ 1
A more complex way is to write your data into an array. Then write your own method for seperating the array. E.g. create 2 Arrays half the size and fill them with array.copy... but in most cases michals answer is usable, easier, prettier, and simply better.
14th Jan 2018, 3:51 PM
Andreas K
Andreas K - avatar
0
thank a lot!
14th Jan 2018, 3:28 PM
Amir01
Amir01 - avatar