I have a numeric String i want to change the String first three character's by another character
Suppose I have a String like 0333XXXXXXX and +38333XXXXXXX. if the first character of String is +38 i want to replace it with "0" while remaining String remain the same Here is my code
private String modifyNumber(String num) {
if (num.startsWith("+38")) {
num.replaceFirst("+38", "0");}
return num;}
"+38" is not accepting in num.replaceFirst.
You can use string replace method. see below
String number = +9231235410;
String newNumber = number.replace("+92","0");
EDIT:
this one is base on your code
private String modifyNumber(String num) {
if (num.startsWith("+92")) {
num = num.replaceFirst("\\+(92)", "0");}
return num;}
Note:
In java, you need to have double backslash since the \ is a unique
java character.
Java strings are immutable method. you need to assign it to a variable to have the result. num = num.replaceFirst("\\+(\\d{2})", "0")
you can use substr() and concat with '0' like,
String str= "+919025858316";
String newstr ="";
if(str.substring(0,3).equals("+91"))
{
newstr = "0" + str.substring(3);
}
else
{
newstr = str;
}
System.out.println(newstr);
output is 09025858316
Check like this :
String num = "+921231231231"; // OR String num = "01231231231";
if(num.startsWith("+92")) {
String a = "0" + num.substring(3);
System.out.println("" + a);
}else{
System.out.println("" + num);
}
Update:
private String modifyNumber(String num) {
if(num.startsWith("+92")) {
num = "0" + num.substring(3);
}
return num;
}
you can just get the last 10 digit of the string and then you can add the zero.
might be that help you
String number= str.substring(Math.max(str.length() - 10, 0));
number ="0"+number;
from here you can get the correct phone number without any code.
might be this help you.Thanks
There is some standardization on mobile numbers in every country. The mobile number is 10 digit. Only here, the last 10 digits are mobile numbers and rest of the digits are country code so take the last 10 digit and add 0 to the first. Then, your problem will be resolved.
i.e,
String number = "+929918001800";
String newNumber = "0"+number.subString(number.length()-10,number.length());
I hope this one will help you :)
Related
You input a word which is a string. What I want to do is to put the letters in an odd position in a variable and those on an even position in another variable...
But I have been reading online and all I can find is how to split by a specific character like: "/", "-" or "". But I dont have one.. show what should I use...
Should I solve this in an other way....
EX:
String S = "alfabet";
and I want to print out:
odd = "afbl";
even = "lae";
System.out.println(odd + " " + even);
I used two strings called odd and even and set both of them to be empty then i iterate throught all the letters of the string s and add the even characters to even and odd characters to odd like the following:
String S = "alfabet";
String odd="";String even="";
for(int c=0;c<S.length();c++)
{
if(c%2==0)odd+=S.charAt(c);
else even+=S.charAt(c);
}
Please do the following:
int i = 0;
StringBuilder oddString = new StringBuilder();
StringBuilder evenString = new StringBuilder();
while(i++ < S.length())
{
if(i & 1){
oddString.append(S.charAt(i));
}else{
evenString.append(S.charAt(i));
}
}
System.out.println("Even String: " + evenString);
System.out.println("Odd String: " + oddString);
i have a large String which includes numbers and text.
Now i want exactly a number from that string.
The number starts every time with a '6' and three '0' but then the number can be different digits.
For example here is a try:
String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK POSTCODE 60025";
if(text1.contains("6000"))
{
System.out.println(text1.indexOf("6000"));
}
So as you can see the String can also contains postcode digits and ids.
But the number i want has always the same length of 9 digits and starts with '6000...'.
So how can i extract that number?
Thanks
EDIT
Ok now i try this one:
String index = "6000";
String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME BECK POSTCODE 60025";
System.out.println(text1.indexOf(index));
String number = text1.substring(text1.indexOf(index), text1.lastIndexOf(text1.indexOf(index) + 5));
System.out.println(number);
It starts but ends not correctly
Regex can be used like this :
public static void main(String args[]) {
String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK POSTCODE 60025";
System.out.println(text1.replaceAll(".*?\\b(6000\\d+)\\b.*", "$1")); // replace everything except a number that starts with 6 and is followed by 000 with "".
}
O/P :
600026821
Note : You can use (6000\\d{5}) instead of (6000\\d+) if you are certain that the number of digits will be 9.
for (String word : s.split(" ")) {
int number = 0;
if (word.startsWith("6000"))
number = Integer.parseInt(word);
}
}
EDIT
I hadn't read that the number you wanted is always of length 9. In that case, check its length in the if condition:
if (word.startsWith("6000") && word.length() == 9)
Like that:
System.out.println(text1.substring(text1.indexOf("6000"),text1 .indexOf("6000")+9));
int value=Integer.parseInt(text1.substring(text1.indexOf("6000"),text1 .indexOf("6000")+9));
Once you have the index of the "6000" just continue, you already said that the length is always 9
int start = text1.indexOf("6000");
String yourNumber = text1.substring(start, start+10);
You can do it like this to get all numbers in that string that follow your rules. Note that I added some more numbers to text1 for testing purposes.
package so;
public class NumberExtractor {
public static void main(String[] args) {
String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK 600026822600026823 POSTCODE 60002682460025";
boolean notAtTheEnd = true;
int currentIndex = 0;
while (notAtTheEnd) {
int start = text1.indexOf("6000", currentIndex);
if (start > -1) {
String number = text1.substring(start, start + 9);
currentIndex = start + 1;
System.out.println(number);
} else {
notAtTheEnd = false;
}
}
}
}
Are there any resources available in the java documentation, or elsewhere that can lead me in the right direction on this? Even just giving the method(s) would help greatly.
Let's say I prompt a user to enter an integer i greater than 0. Then, in the output, they get a string that is the length of the integer. The characters can range from "!" to "~" on the ASCII table (33-126).
For example:
"Enter an integer greater than 0: "
Input: 8
Output: &3lR(c$2
"Enter an integer greater than 0: "
Input: 4
Output: I*#f
Etc, etc...
I think I can figure out the rest myself, such as generating a random (a real random, not a pseudo-random), doing the necessary loop to print an error message if the input is less than or equal to 0. I would prefer method hints over code anyway.
Thank you.
Use a loop with StringBuilder:
String randomString(int lengthOfString){
int minChar = 33;
int maxChar = 126;
StringBuilder result = new StringBuilder();
for (int i = 0; i < lengthOfString; i++){
result.append((char) generateRandomBetweenTwoNumbers(minChar, maxChar));
}
return result.toString();
}
Once you use your random number generator you can just construct the String by converting the int's to char's:
String output = (char)40 + "" + (char)60....
Here's a way to do it using code a beginner would have seen; I don't recommend this.
import java.util.Random;
public class stack {
public static void main(String[] args) {
int length = 8;
char character = 0;
String output = "" + character;
Random random = new Random();
for (int i = 0; i < length; i++) {
character = (char) (random.nextInt(126 - 33 + 1) + 33);
output += character;
}
System.out.println(output);
}
}
alright so im wondering how can I read an integer and string from the same line? Ill give an example:
if I have input=3K how can I make my output look like this: 3K=3000?
Start by breaking down you requirements.
You have an input value of [number][modifier]
You need to extract the number from the modifier
You need to apply the modifier to the number
If you want a variable/flexible solution, where you can supply any type of modifier, you will need to determine the number of digits the user has entered and then the modifier.
Once you have that, you can split the String, convert the digits to an int and apply the appropriate calculations based on the modifier...
Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
int index = 0;
while (index < input.length() && Character.isDigit(input.charAt(index))) {
index++;
}
if (index >= input.length()) {
System.out.println("Input is invaid");
} else {
String digits = input.substring(0, index);
String modifier = input.substring(index);
int value = Integer.parseInt(digits);
switch (modifier.toLowerCase()) {
case "k":
value *= 1000;
break;
//...
}
System.out.println("Expanded value = " + value);
}
String input = "3k";
String output = input.replaceAll("[Kk]", "000");
int outputAsInt = Integer.parseInt(output);
I tried this code but it splits based on country code.
String phone ="1234567891";
String formattedNumber = PhoneNumberUtils.formatNumber(phone);
but this displays as 123-456-7891
I want to displayed like this:
123.456.7891
How can I do this?
Try
String phoneNum = "1234567891";
System.out.println(phoneNum.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "$1.$2.$3"));
If you are sure about the count of the digits then you can insert the dots at certain positions:
String str = "12345678910";
str = str.substring(0, 3) + "." + str.substring(3, 6) + "." +
str.substring(6, x.length());
You can use string replace:
String phone ="1234567891";
String formattedNumber = PhoneNumberUtils.formatNumber(phone);
String formattedNumberWithDots = formattedNumber.replaceAll("-", ".");
For string operations(like concatenation) it is always better to use StringBuilder or StringBuffer.
String str = "1234567890";
if (str.matches("\\d{10}")) {
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.insert(3, ".");
strBuilder.insert(7, ".");
System.out.println(strBuilder.toString());
} else {
System.out.println( str + " is not a phone number");
}