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]);
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 2 years ago.
Improve this question
I am trying to find a way to create a program where users input their full name(first last) and that can remove multiple characters from the second character to another specific character which is the space with StringBuilder. Meaning it will print out the first initial and the entire last name.
Example:
Input:
Barrack Obama
Output:
BObama
You can either use two substrings, which will create two intermediate String objects, or you can use a StringBuilder object as follows:
String input = "Hello everyone, I'm Cho.";
String output = new StringBuilder(input).delete(5, 14).toString(); // "Hello, I'm Cho."
The code below will delete from the second character until the first space detected. For example from Dong Cho to DCho
Scanner scanner = new Scanner(System.in);
String userName;
System.out.println("Enter username");
userName = scanner.nextLine();
int spaceIndex = userName.indexOf(" ")+1;
String firstPartOfString = userName.substring(0, 1);
String lastPartOfString = userName.substring(spaceIndex, userName.length());
userName = firstPartOfString +lastPartOfString;
System.out.println(userName);
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(":");
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 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
}
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
hello I have this string that is a data table I want to test if the string contains more than _ then we are cutting the chains and in each sub string in a table row
I use talend and my input is an excel
I'm really stuck who can help me please
exemple:
JEN3030_1_2_3
JEN3030_1
JEN3030_2
JEN3030_3
Try String.split()...
String input = "JEN3030_1_2_3"; //or whatever
List<String> output = new ArrayList<String>();
String[] parts = input.split("_");
for(int i = 1; i < parts.length; i++) {
output.add(parts[0] + "_" + parts[i]);
}