I am building a new string using string builder. But now if want to add new characters in between already existing characters in the stringbuilder. How do i do it?
Example code:
StringBuilder sbr = new StringBuilder(" ");
sbr.append(1);
sbr.append(" ");
sbr.append(2);
sbr.append(" ");
sbr.append("3");
sbr.append(" ");
Now the string looks like 1 2 3
I want to add a new string after the number two. Can anyone please guide me how to do that?
Use the following to insert the character at position 3
sbr.insert(2, "<new charactor>");
Related
I have a ";" delimited string. I need to remove an entry from it. I tried to use slice but that does get sliced string but not the original-modified string.
Here is an example:
var str1: string = 'TY66447;BH31496;PA99001;';
var str2 = str1.slice(16, 23);
console.log(str1);
console.log(str2);
It gives:
TY66447;BH31496;PA99001;
PA99001
But what I want to achieve is TY66447;BH31496;
I am not sure if I am using the correct string method. Please guide how to achieve.
I don't understand what do you want, but it seems that you want:
str1.slice(0, 16);
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));
}
I am trying to extract the zip code from an address string but it contains newline \n characters. May I ask how to remove it from a selenium stored var? I have tried to use storeEval | "${Addrsss}".replace("\n", "") | Address. But, selenium ide will return the error Threw an exception: unterminated string literal
Here is the address:
${Address} = "100 RILEY DR\n AVONDALE,\n ARIZONA\n 85323-2004"
Try this sequence of escape>replace>unescape as a workaround to remove the new line character:
Escape the value,
Replace the escaped new line character (%0A) with blank (''),
Unescape back to the original value,
storeEval | unescape(escape(storedVars['has_nl']).replace(/%0A/g,'')) | no_nl
This new line character appears to have come from HTML break tag (<br />) that is rendered by the browser-Selenium-IDE combination, then extracted by Selenium IDE as new line character (\\n).
Possible approach:
1) find proper css (or xPath) locator of the element (address)
2) then get contents ( text) from element using
String cssSelecotr=..blablabla..
//1st way
String myAddress=driver.findELement(by.cssSelector(cssSelector)).getText();
//2nd way, using js executor
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssSelecotr+"\");");
stringBuilder.append("return x.text().toString();") ;
String myAddress= (String) js.executeScript(stringBuilder.toString());
3) then you can apply regExp ( all visible symbols) for yourAdress
// myAddress ="100 RILEY DR\n AVONDALE,\n ARIZONA\n 85323-2004";
String myAdressEdited = myAddress.replaceAll("[^\\x20-\\x7E]+","");
Hope this helps
I am trying to create a string mutation that, after a prompt for a city and state, would output the state in uppercase, followed directly by the city in lowercase, followed directly by the state again in uppercase.
I have tried many types of mutations but nothing is working.
Can anyone help me?
use String#toUpperCase() and String#toLowerCase() methods.
eg. System.out.println(state.toUpperCase());
Here is one in java
Scanner sc =new Scanner(System.in);
String city,state;
System.out.println("Enter City =");
city=sc.nextLine();
System.out.println("Enter State =");
state=sc.nextLine();
System.out.println( state.toUpperCase() + " "+city.toLowerCase() + " "+ state.toUpperCase());
Independent of any programming language(But dependent on character Representation) you could retrieve every character of string and add 22 which would convert UPPERCASE into LOWERCASE.As you must be knowing ASCII values of a-z is 97-122 and that of A-Z is 65-90.so you can lookout how much to add/ subtract to convert between cases.
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