Let's say there are three strings:
String s1 = "6A";
String s2 = "14T";
String s3 = "S32";
I need to extract numeric values (i.e. 6,14 and 32) and characters (A,T and S).
If the first character was always a digit, then this code would work:
int num = Integer.parseInt(s1.substring(0,1));
However, this is not applicable to s2 and s3.
You can make something like that:
public static int getNumber(String text){
return Integer.parseInt(text.replaceAll("\\D", ""));
}
public static String getChars(String text){
return text.replaceAll("\\d", "");
}
public static void main(String[] args) {
String a = "6A";
String b = "14T";
String c = "S32";
System.out.println(getNumber(a));
System.out.println(getChars(a));
System.out.println(getNumber(b));
System.out.println(getChars(b));
System.out.println(getNumber(c));
System.out.println(getChars(c));
}
Output:
6
A
14
T
32
S
Try this:
String numberOnly = s1.replaceAll("[^0-9]", "");
int num = Integer.parseInt(numberOnly);
Or the short one:
int num = Integer.parseInt(s1.replaceAll("[^0-9]", ""));
The code is applicable for s2 and s3 as well!
You can use java.util.regex package which is consists two most important classes
1) Pattern Class
2) Matcher Class
Using this classes to get your solution.
For more details about Pattern and Matcher Class refer below link
http://www.tutorialspoint.com/java/java_regular_expressions.htm
Below is the complete example
public class Demo {
public static void main(String[] args) {
String s1 = "6A";
String s2 = "14T";
String s3 = "S32";
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(s3);
while (m.find())
{
System.out.println(m.group());
}
}
}
If you need string and wants to skip numeric value then use below pattern.
Pattern p = Pattern.compile("[a-zA-Z]");
Yo can check whether first character in a string is a letter if yes then do
Integer.parseInt(s1.substring(1))
Means parse from second character
Related
docPreview('https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5')
and I want to remove docPreview('') remains string is my URL.
If your string is always between docPreview(' and '), then you can efficiently substring it out as follows:
String str = "docPreview('https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5')";
System.out.println(str.substring("docPreview('".length(), str.length() - "')".length()));
There are more ways how to achieve this:
Code examples:
package selenium;
public class Ankush {
public static void main(String[] args) {
String s = "docPreview('https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5')";
// split by ' into an array and take second array member
String[] s1 = s.split("'");
System.out.println(s1[1]);
// replace or replaceAll with empty string
String s2 = s.replace("docPreview('", "").replace("')", "");
System.out.println(s2);
// cut substring
String s3 = s.substring(12, s.length() - 2);
System.out.println(s3);
}
}
Output:
https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5
https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5
https://dms.careerbuilder.com/viewer?Token=348861ef265b476d9e689ce2b1174853&key=3e33e4c135b20938f278cd3dd39437c7e6065052281bd9ccba894d655b0460e5
I was trying to split my string using ± (alt + 0177) sign, but it dos't detect it.
I also tried indexOf() but its not work
String myString = "20±1";
if(myString.indexOf('±')>-1){
System.out.println("We are in here.........");
}
You can use the ascii value for the '±' sign.
An easy way get the ascii value as shown in this reply here
In your case:
final int ascii = (int) '±';
final String myString = "20±1";
if(myString.indexOf(ascii)>-1){
System.out.println("We are in here.........");
}
Use function split()
String myString = "20±1";
String result[] = myString.split("±");
//result[0] = 20
//result[1] = 1
/*Your String*/
String myString = "20±1";
/*If you want to split String you can use String.split("your string regex here")
* and it will create String array without specified string regex with left, right
* side of string or multiple strings depending on occurrence of specified string regex*/
String[] splitted = myString.split("±");
/*Just to validate output*/
System.out.println(Arrays.toString(splitted));
you can use also StringTokenizer for this problem:
import java.io.*;
import java.util.*;
class happy {
public static void main(String args[])
{
String myString = "20±1";
StringTokenizer st=new StringTokenizer(myString,"±");
String a="";
while(st.hasMoreTokens())
{
a=st.nextToken();
System.out.println(a);
}
}
}
Let's say I have 2 strings and i need to calculate a difference between their unique characters. It's simple:
String s1 = "abcd";
String s2 = "aaaacccbbf";
//answer: 1
The answer is 1, because there is no "f" in s1 variable.
But what about characters like மா or 漢字, or any other non ASCII character? If i loop though those strings, one character like கு will count 2-3 times as separate character, giving me wrong answer:
String s1 = "ab";
String s2 = "aaaகுb";
//answer: 2 (wrong!)
The code i tried with:
class a {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
sc.close();
String missingCharacters= "";
for(char c : s2.toCharArray()) {
if(!missingCharacters.contains(c+"") && !s1.contains(c+""))
missingCharacters+= c;
}
System.out.println(missingCharacters.length());
}
}
Your symbol கு is compound form of Tamil script which contains two Unicode chars க் + உ (0B95 + 0BC1). If you plan to work with Tamil script you have to find all similiar characters with pattern:
String s1 = "ab";
String s2 = "aaaகுb";
Pattern pattern = Pattern.compile("\\p{L}\\p{M}*");
Matcher matcher = pattern.matcher(s2);
Set<String> missingCharacters=new TreeSet<>();
while (matcher.find()) {
missingCharacters.add(matcher.group());
}
matcher = pattern.matcher(s1);
while (matcher.find()) {
missingCharacters.remove(matcher.group());
}
System.out.println(missingCharacters.size());
Regex source:
How to Match a Single Unicode Grapheme
Set<Integer> missing = new HashSet<>();
for (int i = 0; i < s1.length();) {
int codePoint = s1.codePointAt(i);
if (s2.indexOf(codePoint) == -1) {
missing.add(codePoint); // takes care of duplicates
}
i += Character.charCount(codePoint);
}
System.out.println(missing.size());
கு is a special character, it it formed by merging க and ு, thus creating 2 different characters, and doesn't have 1 single char value. You are looping over the chars in s2, so you won't find that character itself.
Java doesn't have a way around this, as String.substring() and String.charAt() both use chars.
Conclusion, it's impossible to do this with Java's default libraries.
As the question states given the following code:
public class Foo
{
public static void main(String[] args)
{
String test = "Cats go meow";
String[] tokens = test.split(" ");
}
}
is it possible to precompile that regex in the split function along the lines of this:
public class Foo
{
Pattern pattern = Pattern.compile(" ");
public static void main(String[] args)
{
String test = "Cats go meow";
String[] tokens = test.split(pattern);
}
}
Yes, it is possible. Also, make pattern static so the static method main can access it.
public class Foo
{
private static Pattern pattern = Pattern.compile(" ");
public static void main(String[] args)
{
String test = "Cats go meow";
String[] tokens = pattern.split(test);
}
}
According to the docs for the split method in String, you can use String's split or Pattern's split, but String's split compiles a Pattern and calls its split method, so use Pattern to precompile a regex.
No - I think that would be a bad idea!
Looking closely at the source code of the split-method - there is a shortcut implemented in case the string is only of one character (and does not contain a regex-special character)
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
so - split(" ") should be a lot faster.
On the other hand when using regexes it is always a good idea to make them static final members.
edit:
The source code JDK1.7 and OpenJDK 7 seems to be identical for String.split - have a look yourselves:
Lines 2312ff.
So - for more complicated patterns (1 or more spaces for instance):
static final Pattern pSpaces = Pattern.compile("[ ]+");
public class Foo
{
private static final Pattern pattern = Pattern.compile(" ");
public static void main(String[] args)
{
String test = "Cats go meow";
String[] tokens = pattern.split(test);
}
}
Use Pattern.split() instead:
String[] tokens = pattern.split(test);
I have a String
String test = "7462356098660AE";
I want to split it into:
test1 = "746";
test2 = "2356";
test3 = "0986";
test4 = "60AE";
The length of the String test will be always the same.
How will I be able to do this?
PS: I have checked other questions but couldn't find a suitable answer.
String test = "7462356098660AE";
String test1 = test.substring(0,3);
String test2 = test.substring(3,7);
String test3 = test.substring(7,11);
String test4 = test.substring(11,15);
For convenience, you can write this kind of Splitter class:
public class Splitter {
private final int[] borders;
private final String s;
public Splitter(String s, int... borders) {
this.s = s;
this.borders = borders;
}
public String seg(int seg) {
return s.substring(seg == 0? 0 : borders[seg-1], borders[seg]);
}
}
You'd use it like this:
final Splitter splitter = new Splitter("7462356098660AE", 3, 7, 11, 15);
for (int i = 0; i < 4; i++) System.out.println(splitter.seg(i));
Not sure what your use case is here, but if the substring of code are always the same length, you could use the substring() method. You just give it the beginning and ending index of the substring you want.
If the string is fixed width for all of the fields you want, you could use the substring/subSequence methods of String, or your could create regular expression and grabs the values of the capturing groups.