I am storing below number as a string. How can I remove the first three characters & last one character from the string?
String No = "00098000002208";
The easiest and probably fastest way would be to just use the base string function substring:
String input = "00034000004409";
String output = input.substring(3, input.length()-1);
We could also try doing a regex replacement:
String output = input.replaceAll("^.{3}(.*).$", "$1");
Related
I need your help to turn a String like 12345678 into 1234.56.78
[FOUR DIGITS].[TWO DIGITS].[TWO DIGITS]
My code:
String s1 = "12345678";
s1 = s1.replaceAll("(\\d{4})(\\d+)", "$1.$2").replaceAll("(\\d{2})(\\d+)", "$1.$2");
System.out.println(s1);
But the result is 12.34.56.78
If you are sure that you'll always have the input in the same format then you can simply use a StringBuilder and do something like this:
String input = "12345678";
String output = new StringBuilder().append(input.substring(0, 4))
.append(".").append(input.substring(4, 6)).append(".")
.append(input.substring(6)).toString();
System.out.println(output);
This code creates a new String by appending the dots to the sub-strings at the specified locations.
Output:
1234.56.78
Use a single replaceAll() method with updated regex otherwise the second replaceAll() call will replace including the first four digits.
System.out.println(s1.replaceAll("(\\d{4})(\\d{2})(\\d+)", "$1.$2.$3")
This puts dots after every pair of chars, except the first pair:
str = str.replaceAll("(^....)|(..)", "$1$2.");
This works for any length string, including odd lengths.
For example
"1234567890123" --> "1234.56.78.90.12.3"
Please note that this question is not a duplicate.
I have a String like this:
// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: 😀
System.out.println(unicodeCharacter);
How can I convert this String to the unicode character 😀? I then want to show this in a TextView.
What you are trying to do is to print the unicode when you know the code but as String...
the normal way to do this is using the method
Character.toChars(int)
like:
System.out.print(Character.toChars(0x1f600));
now in you case, you have
String myString = "U+1F600";
so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)
Example:
String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));
Try
yourTextVIew.setText(new String(Character.toChars(0x1F60B)));
I have one string which i need to divide into two parts using regex
String string = "2pbhk";
This string i need to divide into 2p and bhk
More over second part should always be bhk or rk, as strings can be one of 1bhk, 5pbhk etc
I have tried
String pattern = ([^-])([\\D]*);
You can use the following regex "(?=bhk|rk)" with split.
str.split("(?=bhk|rk)");
This will split it if there is one of bhk or rk.
This should do the trick:
(.*)(bhk|rk)
First capture holds the "number" part, and the second bhk OR rk.
Regards
String string = "2pbhk";
String first_part, second_part = null;
if(string.contains("bhk")){
first_part = string.substring(0, string.indexOf("bhk"));
second_part = "bhk";
}
else if(string.contains("rk")){
first_part = string.substring(0, string.indexOf("rk"));
second_part = "rk";
}
Try the above once, not using regex but should work.
In case you are looking to split strings that end with rk or bhk but not necessarily at the end of the string (i.e. at the word boundaries), you need to use a regex with \\b:
String[] arr = "5ddddddpbhk".split("(?=(?:rk|bhk)\\b)");
System.out.println(Arrays.toString(arr));
If you want to allow splitting inside a longer string, remove the \\b.
If you only split individual words, use $ instead of \\b (i.e. end of string):
(?=(?:rk|bhk)$)
Here is my IDEONE demo
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use replaceAll.
However, your usage of replaceAll is wrong, it's defined as follows: replaceAll(String regex, String replacement). The correct code in your example would be: replaceAll("[0-9]", "").
You can use the following regex: \d for representing numbers. In the regex that you use, you have a ^ which will check for any characters other than the charset 0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\\d", ""); // or you can also use [0-9]
System.out.println(s);
To remove the numbers, following code will do the trick.
stringname.replaceAll("[0-9]","");
Please do as follows
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
String name = "ram varma656887";
name = name.replaceAll("\\d","");
System.out.println(name);//ram varma
also something like given will work for you
String given = "ram varma656887";
String[] arr = given.split("\\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
try
replaceAll(<your regexp>,"")
you can use Java - String replaceAll() Method.
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Here is the syntax of this method:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
regex -- the regular expression to which this string is to be matched.
replacement -- the string which would replace found expression.
Return Value:
This method returns the resulting String.
for your question use this
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));
I have this string,
anyType{image0=images/articles/4_APRIL_BLACK_copy.jpg; image1=images/articles/4_APRIL_COLOR_copy.jpg; }
What i want is only
"images/articles/4_APRIL_BLACK_copy.jpg"
How do i get this?
This is how I perform a split in my app.
String link = "image0=images/articles/4_APRIL_BLACK_copy.jpg";
String[] parts = link.split("=");
String first = parts[0];
Log.v("FIRST", first);
String second = parts[1];
Log.v("SECOND", second);
This method will split your string into 2 at the "=" and give you 2 split strings. In your case, the String second is the result you want.
This should work:
s.split("=")[1]
You are splitting the string on = which would return substrings in an array. The second element is what you need.