Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Ex String Param=Value1:100,Value2:2000,Value3:30000:
What is the best way in java to trim the above mentioned string format to get the First element's value?
Result should be 100 (from the above mentioned Ex string param).
Assuming you have a String e = "Value1:100,Value2:2000,Value3:30000";
Your next step would be to analyze its structure. In this case it is very simple key:value which is comma separated.
We have to split at every "," and then ":".
String[] keyValues = e.split(",");
for(String keyValue : keyValues) {
String tupel = keyValue.split(":");
// tupel[0] is your key and tupel [1] is your value
}
You can now work with this. You can add these to a map to access it by name.
Also this How to search a string of key/value pairs in Java could be worth looking at.
If you only want the first value, you can take a substring up to the first ',':
String p = "Value1:100,Value2:2000,Value3:30000:";
int firstComma = p.indexOf(',');
if(firstComma >= 0) {
p = p.substring(0, firstComma);
}
String tuple[] = p.split(":");
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
Running java in my cgi-bin. Via post request. In my java program, I have the browser output in a string e.g.: name=josh&age=34.... and so on
In my java program String x = "name=joshua";
How can I split this x string by the = delimiter into a hashtable.
My hashtable is Hashtable<String, String>
You may try something like this:
// initializes a hashtable for key and value types to be String
Hashtable<String, String> h =
new Hashtable<String, String>();
// your string
String x = "name=josh";
// splits the string for "=" delimiter and stores in an array
String[] arrOfStr = x.split("=", 0);
// Use the 'put' method of Hashtable to insert the 0th element of array as key and 1st element as value
h.put(arrOfStr[0],arrOfStr[1]);
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
I'm trying to split a String at every Nth occurence, but missing the last values.Here is what is expected.
Input : String str = "234-236-456-567-678-675-453-564";
Output :
234-236-456
567-678-675
453-564
Here N=3, where the str should be split at every 3rd occurence of -.
Try this.
String str = "234-236-456-567-678-675-453-564";
String[] f = str.split("(?<=\\G.*-.*-.*)-");
System.out.println(Arrays.toString(f));
result:
[234-236-456, 567-678-675, 453-564]
You can try the following with Java 8:
String str = "234-236-456-567-678-675-453-564";
Lists.partition(Lists.newArrayList(str.split("-")), 3)
.stream().map(strings -> strings.stream().collect(Collectors.joining("-")))
.forEach(System.out::println);
Output:
234-236-456
567-678-675
453-564
Maybe one of the worst way without using function available in java , but good like exercise :
public static void main(String[] args){
String s = "234-236-456-567-678-675-453-564";
int nth =0;
int cont =0;
int i=0;
for(;i<s.length();i++){
if(s.charAt(i)=='-')
nth++;
if(nth == 3 || i==s.length()-1){
if(i==s.length()-1) //with this if you preveent to cut the last number
System.out.println(s.substring(cont,i+1));
else
System.out.println(s.substring(cont,i));
nth=0;
cont =i+1;
}
}
}
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 8 years ago.
Improve this question
I have a string Saxon Securitie/Logo/horse-logo.jpg_1413458235818 in format "A/B/C"
I want the result as C by removing "A/B/" from the above string and get a result
String C = "horse-logo.jpg_1413458235818"
Try:
String s = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
String c = s.substring(s.lastIndexOf("/") + 1);
System.out.println(c);
String filePath = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
String fileName = new File(filePath).getName();
See https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
You can use String.lastIndexOf to do that :
String path = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
int index = path.lastIndexOf("/");
String fileName = index == -1 ? null : path.substring(index + 1);
I'm not going to give you answer but you could easily use split function in java that you can learn about here. and at first split with space then split with /
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 8 years ago.
Improve this question
I would like to know if there is a way to extract verbs from a string.
As an example :
I'll eliminate you.
I want it to only extract "eliminate".
You probably want to be looking at using the Stanford PoS tagger (http://nlp.stanford.edu/software/tagger.shtml). It will tag the string with it's classification (e.g. verb, noun). You can then use a regular expression to pull out the verbs.
ArrayList<String> verbs = new ArrayList<>();
verbs.add("eliminate");
String yourtext = "I'll eliminate you";
for (String verb : verbs) {
int index = yourtext.indexOf(verb);
if (index >= 0) {
System.out.println("Found verb: " + verb + " at position: " + index);
}
}
I'll leave filling the verbs array up to you.
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 8 years ago.
Improve this question
Consider this piece of Java code
I need to do this in php
String SEPARATOR = "S39Er#T0R";
String input = "someS39Er#T0RDataS39Er#T0Rhere";
String[] val = input.split(SEPERATOR);
for (int i = 0; i < val.length; i++) {
}
I need to store the data received from file_get_contents('php://input') which returns the raw post
and then from that string I need it to split and run a for loop
Can any one suggest how to implement the same code in php ?
explode function splits a string by a string;
preg_split splits a string by a regex;
foreach construct iterates over arrays or objects;
Combination of these is pretty straightforward:
$separator = 'S39Er#T0R';
$postData = file_get_contents('php://input');
$splitPostData = explode($separator, $postData);
foreach($splitPostData as $postDataItem)
{
// do something with $postDataItem
}