Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get a character index in a java.lang.String. So, for example, I want to find the '2' character index in the "0123456789" string (the index will be 2). There is any method in the String class to use for this, or a simple code?
Thanks for the answer!
You can iterate through the String and look for a character you seek.
Just use a for loop like this:
String test = "I want to test something";
for(int i=0;i<test.length;i++) {
char t = test.charAt(i);
// do something with char
}
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt%28int%29
index will be -1 if 'a' is not in this string.
Note this code will only find the first 'a' in this string. If you need to find multiple locations of a character, then loop through the string using solution in another answer.
String str = "there is a letter";
int index = str.indexOf('a');
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My String is like this: (,,,test,tester,,java_test,,,). I need to remove the empty values from the list. Could you please share your thoughts for this?
Expected result: (test,tester,java_test).
First remove double commas:
my_string = my_string.replaceAll("(,)+", ",");
Get rid of leading commas:
my_string = my_string.replaceAll("^,", "");
Get rid of trailing commas:
my_string = my_string.replaceAll(",$", "");
All together:
my_string = my_string.replaceAll("(,)+", ",").replaceAll("^,", "").replaceAll(",$", "")
In case the brackets make part of the string, the solution should be as follows:
my_string = my_string.replaceAll("(,)+", ",").replaceAll("^\\(,", "(").replaceAll(",\\)$", ")")
list.removeIf(n -> (n == null || n.isEmpty()));
remove all the null elements and empty elements.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
this is an account number 77787845456464645656547877
accountNumber1.replaceAll( "(desc>||desc\s*[:]{2}\s*?|)(\d(?=\d{4}))", "$1XXXXXXXXXX$2") This is not working,I tried different ways no result .Pl check ,thx
An approach without regex:
char[] cs = accountNum.toCharArray();
Arrays.fill(cs, 0, cs.length-4, '*');
String masked = new String(cs);
One approach is to replace every digit with a *, provided it's followed by four or more other digits. Do the last part with a forward lookup, something like
myString.replaceAll("\\d(?=\\d{4})", "*");
which returns myString with every digit other than the last four replaced with *.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to get a value in a edi file having below format
\nRJCK3:0*20180105*U*127.35
\nRJCK3:0*20180105*B*127.35
I want a value U in 1st case, which comes between 2nd & 3rd star after RJC*K3, and want B in second string
Precisely, want to fetch a single character from a string, where that character will comes between 2nd & 3rd star(*) of a RJC*K3(static value).
You can use a classic Pattern matching way :
String str1 = "\\nRJC*K3:0*20180105*U*127.35";
Matcher m = Pattern.compile("RJC\\*K3.*\\*(\\w)\\*.*").matcher(str1);
String res1 = m.find() ? m.group(1) : "";
System.out.println(res1); // U
But if there is always the same amount of * before the letter you want, you cn easily split and take the 3rd part :
String str2 = "\\nRJC*K3:0*20180105*G*127.35";
String res2 = str2.split("\\*")[3];
System.out.println(res2); // G
There is no need to fight with edi files, you can use available libraries.
Please take a look at https://github.com/imsweb/x12-parser/
RJCloop.getSegment("RJC").getElementValue("RJC02")
can get you the value needed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
There are 3 text fields the user will type into
"To be inserted message" in first Text field
"The message" in second Text field
and a substring of the message in the third text field
The program should be able to insert "To be inserted message" in Message after the substring.
Example ↓
Message→ Stack flow
To be inserted message → over
substring→Stack
Output →
Stack over flow
When do you want to insert substring into the main string you can use insert(int offset, A). But before you need to get the place where you need to put it and we need to REMEMBER that insert() works with StringBuilder. When you don't use it you can use method substring(). From you example, as can be seen, you will point after substring. To do this, we need to find that substring into the main string whereby: indexOf(String str).
Assuming we have: mainSTR, subSTR and str after which we need to input subSTR.
String mainSTR = "Stack flow";
String subSTR = "over";
String str = "Stack";
Let's go to think!
int pos_str = mainSTR.indexOf(str)+str.length();
pos_str will point us for place. Also, for first input str we added its length. It needed to point the space after "Stack". Now we can input subSTR in mainSTR.
Here is an answer for you question: Insert a character in a string at a certain position
Using substring() you need to take, at first, first part of main string, later add subSTR and later add second part of mainSTR.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm developing an application where the user will insert an vehicle ID.
At our country, the ID is always 3 letters and 4 numbers.
How can I check if a String has X numbers and Y letters in Java?
You can simply work with regular expressions. Something like:
if(vehicleId.matches("^[A-Z]{3}\d{4}$"))
(Assuming the id contains three capital letters followed by four digits.)
This will return a boolean if vehicleId, supposedly a variable holding the user's input, is matched.
There are many solutions for this problem. I'll not give you a full solution but will try to guide you.
One solution would be iterating on the string char-by-char. The Character class contains many useful methods for this task.
Other solution would be using a regex and replaceAll non digits characters (\D) with the empty string. From here the way to the answer is very short.
Visit the String API and the regex tutorial to fuel your creative fire.