given a string like this
string a = "Course Name:\n" + CourseName
+"\n\nVenue:\n" + locationName
+"\n\nStart Time:\n" + startTime
+"\n\nEnd Time:\n"+ endTime";
CourseName,locationName,startTime and endTime would be a variable and being assigned to string a, how can i get all these value from the string using substring or regex and store all of it into different variable? please note that I don't have access all these variable, I can only access the string which leave me the only option is play around with substring or regex.
You could split by newline
String[] parts = a.split("\\n");
String courseName = parts[1];
String locationName = parts[4];
String startTime= parts[7];
String endTime= parts[10];
or you can use:
StringTokenizer tokens=new StringTokenizer(a, "\n");
while(tokens.hasMoreTokens()){
System.out.println(tokens.nextToken());
}
Related
I need some help with these strings. My code is:
String string = database.getCurrentDate();;
String[] array = string.split("[.]", 0);
String date = array[0] + "." + array[1];
String string1=database.getCurrentHour();
String[] array1=string1.split("[:]",0);
String hour=array1[0]+":"+array1[1];
String finalString=date+hour;// finalString is 28.0310:45
I need that finalString to be:
date
hour//on next line
example:
28.03//date
10:45//hour
The point is that I need the hour to be displayed on the next line, under date.
Thank you!
String finalString = date + "\n" + hour
Don't work if you display this?
I have a little problem with splitting a String
String anl_gewerk = "Text Text Text (KG 412/2)"
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
anl_gewerk = anl_gewerk.replaceAll("\\(","").replaceAll("\\)","");
anl_gewerk = anl_gewerk.replace("KG","");
I have the aforementioned string, and I'm searching for "412/2".
Therefore, I want to split the String into two substrings searching for "(".
Finally I want to grab this String deleting "(", "KG", " " and ")".
When I select anl_gewerk = parts[0]; it works but I get the wrong part, when I change into parts[1] the App crashes.
Please help me
Try to change your code by this:
String anl_gewerk = "Text Text Text (KG 412/2)";
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
String sub[] = anl_gewerk.split(" ");
String test = sub[1];
String result = test.replace(")","");// it gives your result 412/2
I have a problem with String.format In android I want replace { 0 } with my id.
My this code not working:
String str = "abc&id={0}";
String result = String.format(str, "myId");
I think you should use replace method instead of format.
String str = "abc&id={0}";
str.replace("{0}","myId");
you have 2 ways to do that and you are mixing them :)
1.String format:
String str = "abc&id=%s";//note the format string appender %s
String result = String.format(str, "myId");
or
2.Message Format:
String str = "abc&id={0}"; // note the index here, in this case 0
String result = MessageFormat.format(str, "myId");
You have to set your integer value as a seperate variable.
String str = "abc&id";
int myId = 001;
String result = str+myId;
try this,
String result = String.format("abc&id=%s", "myId");
edit if you want more than one id,
String.format("abc&id=%s.id2=%s", "myId1", "myId2");
The syntax you're looking for is:
String str = "abc&id=%1$d";
String result = String.format(str, id);
$d because it's a decimal.
Other use case:
String.format("More %2$s for %1$s", "Steven", "coffee");
// ==> "More coffee for Steven"
which allows you to repeat an argument any number of times, at any position.
In this json i have two image path
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg"
and second
String imgPhoto="Jellyfish82238.jpg" so I want the first String in this abc.jpg replcae with Jellyfish82238.jpg and parse in JSON so please help me.
We assume that the URL is dynamic in every time. We assume also that the base URL to get images is
http://eonion.in/vimalsagarji/static/eventimage/
In this case, we could play with split to perform what you want
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg"
String[] parts = string.split("/");
So, then we have :
String part1 = parts[0]; // http://eonion.in
String part2 = parts[1]; // vimalsagarji
String part3 = parts[2]; // static
String part4 = parts[3]; // eventimage
String part5 = parts[4]; // abc.jpg
Then, we replace part5 by imgPhoto.
kString newImageUrl = part1 + "/" part2 + "/" part3 + "/" + part4 + "/" + imgPhoto
Simply use replace()
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg";
String imgPhoto="Jellyfish82238.jpg";
String url= imgUrl.replace("abc.jpg", imgPhoto);
Simply use replace(). Since replace() will produce a new String, you should assign the new String to imgUrl
imgUrl= imgUrl.replace("abc.jpg", imgPhoto);
Try this:
String imgUrl="http://eonion.in/vimalsagarji/static/eventimage/abc.jpg";
String imgPhoto="Jellyfish82238.jpg";
imgUrl=imgUrl.substring(0,imgUrl.lastIndexOf("/")+1)+imgPhoto;
Log.d("imagevalueclicked",imgUrl);
I have a string that I want to break down and assign different part of this string to different variables.
String:
String str ="NAME=Mike|Phone=555.555.555| address 298 Stack overflow drive";
To Extract the Name:
int startName = str.indexOf("=");
int endName = str.indexOf("|");
String name = str.substring(startName +1 , endName ).trim();
But I can't extract the phone number:
int startPhone = arg.indexOf("|Phone");
int endPhone = arg.indexOf("|");
String sip = arg.substring(startPhone + 7, endPhone).trim();
Now how can I extract the phone number that is between delimiter "|".
Also, is there a different way to extract the name using the between delimiter "=" & the first "|"
You can split on both = and | at the same time, and then pick the non-label parts
String delimiters = "[=\\|]";
String[] splitted = str.split(delimiters);
String name = splitted[1];
String phone = splitted[3];
Note that his code assumes that the input is formatted exactly as you posted. You may want to check for whitespace and other irregularities.
String[] details = str.split("|");
String namePart = details[0];
String phonePart = details[1];
String addressPart = details[2];
String name = namePart.substring(namePart.indexOf("=") + 1).trim();
String phone = phonePart.substring(phonePart.indexOf("=") + 1).trim();
String address = addressPart.trim();
I hope this could help.