This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I am able to capture a file name in either a File or String format.
For example the file paths.
\EAM\testing
\EAM\development
\System\Applications\Management
I need to break these into different strings or substring so they would be
\EAM\testing
String 1: EAM
String 2: testing
\EAM\development
String 1: EAM
String 2: development
\System\Applications\Management
String 1: System
String 2: Applications
String 3: Management
For the first two I know i could possibly use
int index = myStr.lastIndexOf('#');
String firstPart = myStr.substring(1, index);
Any help is greatly appreciated.
String[] parts = myStr.split("\\");
Use String.split() method to get all the values in an array:
String []names = filePathString.split("\\");
for(String name:names) {
System.out.println("name: " + name);
}
Related
This question already has answers here:
substring index range
(9 answers)
Finding substring in RegEx Java
(5 answers)
Closed 4 years ago.
I have a String name packageName="https://play.google.com/store/apps/details?id=com.MoversGames.DinosaurWorldGame";
How i can get subString from this string. I want to take "id=com.MoversGames.DinosaurWorldGame" this from String packgeName. The length of this String may change. It is not constant its variable .
You need to use String.substring()
Try to substring your string from the index of id to length of your string
In this below example i have started substring from index of ? +1 so it will start substring your string from id to length of your string
SAMPLE CODE
String packageName = "https://play.google.com/store/apps/details?id=com.MoversGames.DinosaurWorldGame";
String answer = packageName.substring(packageName.indexOf("?")+1, packageName.length() );
Log.e("ANSWER", answer);
OUTPUT
2018-10-11neel.com.myapplication E/ANSWER: id=com.MoversGames.DinosaurWorldGame
There might be also some more query parameters in your String , so you can also try it like this :
String packageName = "https://play.google.com/store/apps/details?i
id=com.MoversGames.DinosaurWorldGame&x=y&z=a";
String answer = packageName.substring(packageName.indexOf("id"));
System.out.print("ANSWER", answer);
This question already has answers here:
How do I convert strings between uppercase and lowercase in Java?
(6 answers)
Closed 6 years ago.
Here is a string
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
I was wondering (although it is currently one in lower case)
What code would i use to make any string into Just lower or upper case
Many thanks
What about the String toUpperCase() Method ?
String newString = foxes.toUpperCase()
See http://www.tutorialspoint.com/java/java_string_touppercase.htm
There's a method in the String class:
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
String upperCase = foxes.toUpperCase();
String lowerCase = foxes.toLowerCase();
String is immutable, so you don't alter the original; you create new ones.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I am trying to only display data after a certain static word (in)
Example:
String jobName = job.getDescription();
returns the following:
XYZ/LMNOP in ABCEFG
I only want the data after the "in" in this scenario. However the XYZ/LMNOP is different in almost every case so I cannot simply call out that section of the string.
You can use split() in the String class.
String jobName = job.getDescription();
String[] parts = jobName.split("in"); { "XYZ/LMNOP", "ABCEFG" }
String before = parts[0]; // XYZ/LMNOP
String after = parts[1]; // ABCEFG
Find index of "in" in the string and then use the string from that particular index+3 to last.
int k = p.indexOf("in");
System.out.println(p.substring(k+3));
index+3 because "i", "n" , " " are 3 characters.
First you need to understand your strings possible data values. If it is always <some_text> in <some_text> then there are muliple ways as other users have mentioned.
Here is another way, whih is bit simpler
String[] strArray = job.getDescription().split(" "); //splits array on spaces
System.out.println(strArray[2]);
try using this
String search = " in ";
String d = job.getDescription();
d = d.substring(d.indexOf(search) + search.length(), d.length());
Outputs, given the inputs:
[find something in a string] -> [a string]
[finding something in a string] -> [a string] // note findINg, that does not match
The search key can be changed to simply in if desired, or left as is to match the question posted to avoid an accidental in in a word.
If you so choose, you can also use .toLower() on getDescription() if you want to be case insensitive when matching the word in as well.
This question already has answers here:
replace String with another in java
(7 answers)
Closed 8 years ago.
I have string "2x+3" and I want change 'x' to string "8".
String operation = "2x+3";
String x = "8";
And I want result such as "2*8+3"
or
String operation = "x+3";
String x = "8";
And I want result such as "8+3"
Does anyone know how to solve ?
operation = operation.replace("x","8")
Well you can't change the same string, because strings are immutable objects in java. What you can do is use the replace function in string class and get a new string out of it.
Have a look at this question : replace String with another in java
This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.