Groovy script remove newline from StringBuffer - groovy

Thanks for taking the time to look into this.
How do I get rid of all newline characters from a StringBuffer?
The file that i'm reading in looks something like this -
USER|SLACK|TRELLO|BINARY|YO!##!
1234|Joe|||10001!##!
3212|Test1|||10001!##!
2213|Tin Man||24|10001!##!
I tried to use replaceAll(pattern,closure) like shown below. But somehow cant quite get my head around it.
package carriageReturnRemove
class asdf {
static void main(def args){
String str = new File('C:/org.txt').getText()
StringBuilder sb = new StringBuilder(str)
sb = sb.replaceAll(~'/n','')
}
}
The output basically needs to look like this -
USER|SLACK|TRELLO|BINARY|YO!##!1234|Joe|||10001!##!3212|Test1|||10001!##!2213|Tin Man||24|10001!##!
Where am I going wrong? Any help or pointers are greatly appreciated.
Cheers.

Get the lines and join them
new File('C:/org.txt').readLines().join()

You may use this too.
new File("C:/org.txt").text.replaceAll("[\r\n]+","")

Related

IwNUI CTextFieldPtr to string

I'm using a IwNUI CTextFieldPtr control and I would like to store/use the text attribute stored on the object in a string variable. I need to use that string but I have no clue on the documentation or examples on how to do it... I don't have a complete code sample either because what I'm asking should be pretty straight forward, such as:
CTextFieldPtr login_tUsername;
//textfield init here
std::string c_username;
login_tUsername->GetAttribute("text", c_username);
Please help me, thank you very much!
And other approach, would be something like this, which is by far much closer to what I wanted to do:
CString value;
login_tUsername->GetAttribute("text", value);
std::string thestring = value.Get();
:)
(Credit goes billarhos billarhos)
Well... the best solution I found for this problem was something like this:
login_tUsername->SetEventHandler("textchanged", this, &OnUserEdit);
bool OnUserEdit(CTextField* textField, const char* text)
{
c_username = text;
return true;
}
I don't know why, but it seems you cannot use the textbox text attribute directly.
Cheers!

Start and stop parsing of a string

I grab a url and I want to parse and store two sections of the url
User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
So I want to start at (Confirmation?=) and stop at (&) and store the results
string = QVNERkFTREY
then for the second one I want to start at (&code=) and go to the end of the string and store that result
string = MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
I was have tried a few different things
Uri myUri = new Uri(Request.Url.AbsoluteUri);
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("Confirmation?=");
string param2 = myUri.Query.Split();
Pretty sure I should be going a different route here but any help would be appreciate. I am going to continue to google search for now. I appreciate the help.
EDIT: I feel as though LINQ should be able to help me here..hmm
I would use HttpUtility.ParseQueryString rather than try to parse the URL myself.
String val = "User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==";
System.Collections.Specialized.NameValueCollection parameters = System.Web.HttpUtility.ParseQueryString(val);
Console.Out.WriteLine(parameters[0]); // QVNERkFTREY=
Console.Out.WriteLine(parameters.Get("code"); // MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==
You will need to add System.Web.dll, which you can read about here: Cannot add System.Web.dll reference

Playn text wrapping and style issue

As in Effect.shadow() is deprecated in PlayN1.3.So i had something like this before :
TextFormat textFormat = new TextFormat(myFont, textWidth, Alignment.LEFT, colorCode, Effect.shadow(-16777216, shadowX, shadowY));
So i changed it to this :
TextFormat textFormat = new TextFormat();
textFormat.withFont(myFont);
textFormat.withWrapping(textWidth, Alignment.LEFT);
I dont want shadow now.It's ok but i did not get previous like result.Hold on.dont think now.Then i changed this code to this:
TextFormat textFormat = new TextFormat().withFont(myFont).withWrapping(textWidth, Alignment.LEFT);
It gives me result as previous except shadow which i dont care now.If i am not wrong this is one line representation of above code.Is not it?
So why it worked and above code did not.Any conceptual difference is there? Anyone can explain please!
//note: dont worry about variables(textWidth,myFont)they are nothing to do with this.
TextFormat objects are immutable. When you call textFormat.withFont(myFont) that returns a new TextFormat instance, which the code above is throwing away. If you want the first code to work you need to write it like this:
TextFormat format = new TextFormat();
format = format.withFont(myFont);
format = format.withWrapping(textWidth, Alignment.LEFT);

How to implement string manipulation in efficienct way?

I have a string ="/show/search/All.aspx?Att=A1". How to get the last value after the 'Att=' in efficient way ?
You could do a split on the '=' character.
Example (in C#):
string line = "/show/search/All.aspx?Att=A1";
string[] parts = line.Split('=');
//parts[1] contains A1;
Hope this helps
If you're only dealing with this one URL then both of the other answers would work fine. I would consider using the HttpUtility.ParseQueryString method and just pull out the item you want by key.
Whatever an
efficient way
is...
Try this:
var str = "/show/search/All.aspx?Att=A1";
var searchString = "Att=";
var answer = str.Substring(str.IndexOf(searchString) + searchString.Length);

preg_replace: reference object in replacement

Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );

Resources