Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have the following information
String templateString = "I am %NAME% and I live in %PLACE%";
String inputString = "I am John Doe and I live in New York";
I need to write a function which will take in the above 2 strings and return a HashMap of pairs.
HashMap<String,String> parseInputFromTemplate(templateString, inputString) {
// Magic
return result;
}
The result will have
%NAME% (key) , John Doe (value)
%PLACE% (key) , New York (value)
Any pointers would be appreciated.
You can use regular expressions to extract what you need. Alternatively you can use the split() method to split on "%". Every other string in the resulting array would be a template var. The others would be the static strings to discard in the inputString.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Any ideas how to effeciently convert the String "TDMaturityReinvestOnNotSelected" to "TD Maturity Reinvest On Not Selected" using a java function?
Cheers
Shaun
This brilliant answer to RegEx to split camelCase or TitleCase (advanced) should work nicely.
Below is an excerpt from that answer:
final String pattern = "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])";
for (String w : "TDMaturityReinvestOnNotSelected".split(pattern))
{
System.out.println(w);
}
And the ouput to show it running:
Edit: You'll need to reassemble the split words with spaces, but that should be trivial to work out.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a string "Hello". I only want all the letters after the first letter to be in my new string.
E.g.: "ello"
How could this be done?
Tried pattern matching but cant get it to work :(
Try this:
String s = "Hello";
String newS = s.substring(1); // newS is "ello"
The above will create a new string containing all the characters of the original, except the first one. See the documentation for more details.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am working n this lab for school and have am doing something wrong with my toString
this is what i have
public String getRelatives(String person)
{
String s = "";
s+=(person);
s+=(" is related to ");
for(String relative : map.get(person))
{
s+=(relative);
s+=(' ');
}
return s;
}
/**
* returns the String version of the entire map listing each key person and all of
* their relatives
*/
public String toString()
{
String output="";
return getRelatives();
}
i want it too look like this
Bob is related to John Tom
Dot is related to Chuck Fred Jason Tom
Elton is related to Linh
i know i am doing something wrong in my toString but to me at least it makes sence and am not sure how i should do it
The declared method getRelatives() takes a String as an argument. When you are calling it in the toString() method, you are not passing a String argument to it. Therefore, it gives you an error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For this string, 16.82080560, 96.13055810 I want to get
"String one = 16.82080560"
and
"String two = 96.13055810"
in android.
Admin that I suck in string manipulation and regex.
Please let me know how can I get such two value from a string.
String[] components = original.split(",");
If the Strings are always separated by a comma you can use String.split
For a better regex pattern see the comment from #npinti:
Minor side note, it might be better to do \\s*,\\s* instead of just ,.
Just , might cause problems should the OP wish to cast these to
floats, since the extra white space at the beginning of the second
number will most likely not be recognized as a proper number.
Another option:
String str = "16.82080560, 96.13055810";
StringTokenizer st = new StringTokenizer(str,", ");
String one = st.nextToken();
String two = st.nextToken();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();