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
Related
I am trying to clean text strings containing any ' or ' (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell's...
To remove the ' and ' ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("' ;","")
any advice is welcome, best regards
When you try to replace("' ;","") it literally searching for "' ;" occurrences in string. You need to convert "' ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('''[2:])), "")
and with this chr(int('''[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('''[2:])), "") without saving your result in variable then your original string would not be affected.
I am facing an issue when trying to replace the newline character /n from a json string .
Here iam serializing my response data using JsonConvert and then trying to find and replace /n but its not finding any newline characters even if it has multiple.
The serialized text is having around 3k lines
var jsonResponse = JsonConvert.SerializeObject(response);
string formatted = string.Empty;
// Not working
if (jsonResponse.Contains(Environment.NewLine))
{
formatted = jsonResponse.Replace(Environment.NewLine, "");
}
But if i save this above jsonResponse to a .txt file and then read all text to a variable its working fine.Its finding the new line character and then replaces it.
var text = System.IO.File.ReadAllText(#"D:\TestData.txt");
// Working
if (text.Contains(Environment.NewLine))
{
formatted = text.Replace(Environment.NewLine, "");
}
How can i make this work with out loading from a text file.Please suggest
i have a problem to load a String value using ParseConfig from Parse.com with new line character.
The new line character is ignored, is that normal or i'm doing something wrong?
There is any other solution to get a text with new line character without using html code and uiwebview?
This question is old, but I'm posting here for posterity as I had the same issue.
On Parse, use another character such as <br> where you want a new line in your String.
Then, in Swift or Objective C, replace <br> with \n, which is recognized by Swift:
Swift:
// Assume you've loaded a String from Parse, called 'rawStringFromServer'
let newString = rawStringFromServer.replacingOccurrences(of: "<br>", with: "\n")
myUILabel.text = newString
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>");
I have something like:
def newProps = new Properties()
def fileWriter = new OutputStreamWriter(new FileOutputStream(propsFile,true), 'UTF-8')
def lineSeparator = System.getProperty("line.separator")
newProps.setProperty('SFTP_USER_HASH', userSftpHome.toString())
newProps.setProperty('GD_SFTP_URI', sftpHost.toString())
fileWriter.write(lineSeparator)
newProps.store(fileWriter, null)
fileWriter.close()
The problem is that store() method escapes ":" or "=" characters with backslash (). I don't want that because I store there some passwords and tokens and need to copy those values strictly in the key=value format.
Also, when I use the configSlurper, it stores the values with single quotes, like:
key='value'
Is there any solution for that? Saving in unescaped key=value format to properties file in Groovy?
You could do this:
def newProps = new Properties()
newProps.setProperty('SFTP_USER_HASH', 'woo')
newProps.setProperty('GD_SFTP_URI', 'ftp://woo.com')
propsFile.withWriterAppend( 'UTF-8' ) { fileWriter ->
fileWriter.writeLine ''
newProps.each { key, value ->
fileWriter.writeLine "$key=$value"
}
}
BUT, so long as you are reading the properties in with load, there should be no need for this as it should de-escape any escaped characters
The JDK's built in Properties class does that escaping by design. According to the Docs:
Then every entry in this Properties table is written out, one per
line. For each entry the key string is written, then an ASCII =, then
the associated element string. For the key, all space characters are
written with a preceding \ character. For the element, leading space
characters, but not embedded or trailing space characters, are written
with a preceding \ character. The key and element characters #, !, =,
and : are written with a preceding backslash to ensure that they are
properly loaded.
You can however, override this behavior by sub-classing the Properties class yourself. You'd need to override the load and store methods yourself and read/write yourself. It would be pretty straight forward; pretty good examples found here: Link