I have a string similar to below
"OPR_NAME:CODE=value,:DESC=value,:NUMBER=value,:INITIATOR=value,:RESP"
I am using StringTokenizer to split the string into tokens based on the delimiter(,:),I need the values of
CODE,DESC and NUMBER.
Can someone pls tell how to achieve this ? The values may come in random order in my string
For eg my string may be like below as well :
"OPR_NAME:DESC=value,:NUMBER=value,:CODE=value,:INITIATOR=value,:RESP" and still it should be able to fetch the values.
I did below to split the string into tokens
StringTokenizer st = new StringTokenizer(str,",:");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
But not sure how to store these tokens to just get the value of 3 fields as mentioned above.
Thanks !!
Okay so what I meant is, detect where is the "=" and then apply a substring to get the value you want.
rough example
System.out.println(st.nextToken().substring(st.nextToken().indexOf('=')+1,st.nextToken().length()));
Use split instead :
String[] parts = X.split(",:");
for (String x:parts) {
System.out.println(x.substring(x.indexOf('=')+1));
}
Related
Can someone please help me with how can a string value be converted into a predefined format by splitting the string?
For example:
If the
Stringvalue = "20210819"
needs to be converted as "08/19/2021"
(Splitting first four and then next two and again next two)
Thanks,
Thanks, #DaveNewton, I was able to do that as you suggested.
Stringvalue = "20210819"
valuedate=Stringvalue
valueyear=(valuedate[0..3]);
valuemonth=(valuedate[4..5]);
valueday=(valuedate[6..7]);
println (valuemonth+"/"+valueday+"/"+valueyear);
I got some function:
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\n")
.shuffled()
.first()
And if I try to get or print some string with the 2 values obtained from this function, the first value disappears. For example:
println("${selectHometown() ${selectHometown() }")
Will only print one city name, while I expect two. I guess the problem is related to string concatenation in Kotlin. Of course, I can get the desired result in a different way, but I'm wondering why this one doesn't work.
Windows way of terminating a line is to use "\r\n" so use it as delimiter :
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\r\n")
.shuffled()
.first()
println("${selectHometown()} ${selectHometown()}")
I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"
I have correlated the Token value taken from the following response snippet:
result.sessionToken = '7AFF3BA8\x2DD913\x2D4211\x2D990E\x2D7DF3AB5687B7';
Using the web_reg_save_param function as:
web_reg_save_param(
"TOKEN",
"LB=result.sessionToken = '",
"RB=';",
"ORD=1",LAST);
But in a later request I need to send the correlated value in the below format:
7AFF3BA8-DD913-4211-990E-7DF3AB5687B7
The value \x2D is to be substituted by -.
I am right now using the below 'C' and LR code for this:
strcat(pstr1,lr_eval_string("{RToken}"));
strcat(aSeparator,"\\");
for(a=0,b=0;pstr1[a]!=NULL;a++,b++)
{
if(pstr1[a]==aSeparator[0])
{
strcat(pstr2,"-");
pstr2[b+1]=pstr1[a+4];
a=a+5;
b=b+2;
}
pstr2[b]=pstr1[a];
}
lr_save_string(lr_eval_string(pstr2), "sessionToken");
I wanted a generic and another approach for this problem. I don't want to use web_convert_param function, but if there is a hidden trick to convert the string as desired I would like to know.
Thanks,
Ritika
Try This...lr_save_string(lr_eval_string("{TOKEN}"),"convertedtkn");
I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number