I am trying to add multiple spaces in my var but it get's cut down to one space or it renders out & nbsp; as is. I have tried using & nbsp; and %20 any one have any other ideas?
ViewBag.Subheading = "Bringing to light";
I want it to look like this
Bringing to light
ViewBag.Subheading = "Bringing to light".Replace(" ", " ");
And
#Html.Raw(ViewBag.Subheading)
Or you could do something like:
public static MvcHtmlString DisplayAndRetainSpaces(this HtmlHelper html, string value)
{
return MvcHtmlString.Create(value.Replace(" ", " "));
}
Then call it like:
#Html.DisplayAndRetainSpaces(ViewBag.Subheading)
Use the entity for that: for each space you want.
EDIT: If you already tried and didn't work, there's an helper for outputing html, which should work with the entity:
#MvcHtmlString.Create(" ");
Related
I have code like this on the flutter, I need to put parameters data like icon and text, but I got error when put two text, how I can put two type same parameter?
new myCard(icon: Icons.home, text: 'Home', text: '234')
You cannot use the same names for multiple parameters, because they worklike variables for the constructor/method.
What you have to do is create multiple parameters for every text you need. For instance: if you need a title, create a title parameter, for a subtitle, create a subtitle parameter, instead of trying to use title twice.
Another way would be to pass an array of strings if you'll use them together, like to generate a string (check the example below). But don't use it if you want different strings in different places, it wouldn't be a good practice.
Check out this example:
void main(){
print( new Test(title: "Home", description: "The home page", textList: ["a", "list", "of", "strings"]) );
}
class Test {
String title;
String description;
List<String> textList;
Test({String this.title, String this.description,
List<String> this.textList});
#override
String toString() => "Title: " + title + "\nDescription: "
+ description + "\nText list together: " + textList.join(" ");
}
(copy and paste the code in the Dartpad if you want to test it, it simply creates and outputs a test class that receives two string parameters and a list of strings)
I've been battling this for two days and am at a loss. I'm attempting to create node relationships and am severely failing.
Here is my code for creating and running the relationship.
var query = graphClient.Cypher
.Match("(apt_1:AttackPatterns)", "(apt_2:AttackPatterns)")
.Where((AttackPatterns apt_1) => apt_1.Id == Convert.ToInt64(apt.ID))
.AndWhere((AttackPatterns apt_2) => apt_2.Id == Convert.ToInt64(rt.Relationship_Target_ID))
.CreateUnique("(apt_1)-[:" + rtrn.ToString() + "]->(apt_2)");
query.ExecuteWithoutResults();
Here is the AttackPatterns class.
public class AttackPatterns
{
public long Id { get; set; }
public string Name { get; set; }
}
During runtime, the value for query equates, in one iteration, to the following:
MATCH (apt_1:AttackPatterns), (apt_2:AttackPatterns)\r\nWHERE (apt_1.Id = \"1\")\r\nAND (apt_2.Id = \"122\")\r\nCREATE UNIQUE (apt_1)-[:ChildOf]->(apt_2)
I notice the "\r\n" characters. I also notice quotes around 1 and 122. When I paste this into the Neo4j web interface replacing "\r\n" with actual new lines and remove the "\" escape character before the quotes, it fails. If I remove the quotes around the 1 and 122, it successfully creates the relationship.
I'm really not sure what I'm doing wrong and would appreciate any assistance!
I've made some assumptions about types, as I think I know what's going on - basically - the Convert.ToInt64 isn't called before the query is made, you have to do it outside of the query generation:
//Given:
var apt = new { ID = "1" };
//Convert outside query
var ap1Id = Convert.ToInt64(apt.ID);
//Use in query
var query = gc.Cypher
.Match("(apt_1:AttackPatterns)", "(apt_2:AttackPatterns)")
.Where((AttackPatterns apt_1) => apt_1.Id == ap1Id) // <-- using locally created var
/* etc */
The \r\n is just for formatting when you look at the DebugQueryText property, they're not sent across, the only problem is the " around the numbers.
Please feel free to add this as a bug to the github project and I'll have a look into it, ideally it would be executed beforehand, but it's possibly like this for a reason.
I know how to split the string within a Controller or Domain class.
But i want to split the string inside the GSP.
My string will look like:
ASD25785-T
I want to be able to split this into 2 strings inside the GSP view.
String a = ASD25785
String b = T
Is it possible to do that inside the GSP?
How about something like this:
<%
String[] tokens = "ASD25785-T".split("-")
String b = tokens[0]
String c = tokens[1]
%>
NB. use try catch because you may get ArrayOutofBoundException
It depends if you have a predefind format or you want something generic.
Without try/catch and using the regex find method in String:
<%
String s="ASD25785-T"
String a,b
s.find(/(.+)-(.+)/) { fullMatch, first, second -> [
a=first
b=second
}
%>
If you are certain that there will always be a match, then it is a cute one-liner:
<%
String s="ASD25785-T"
def (a,b) = s.find(/(.+)-(.+)/) { fullMatch, first, second -> [first,second]}
%>
Source:
http://naleid.com/blog/2009/04/07/groovy-161-released-with-new-find-and-findall-regexp-methods-on-string
NB: However, if you want to use it in your view, you should create a tag. Grails taglibs are almost trivial to write, and much better to use in GSP code.
http://grails.github.io/grails-doc/2.4.x/ref/Command%20Line/create-tag-lib.html
http://grails.github.io/grails-doc/latest/guide/single.html#taglibs
Here's a string manipulation taglib
class StringsTaglib {
def split = { attrs, body ->
String input= attrs.input
String regex= attrs.regex
int position= attrs.index as Integer
out << input.split(regex)[position]
}
}
you could then use it like this:
a:<g:split input="ASD25785-T" regex="-" index="0"/>
b:<g:split input="ASD25785-T" regex="-" index="1"/>
I would like to create a helper to return a text one character below the other. Something like that:
S
A
M
P
L
E
The purpose of this helper is to have a table with a heading of only 1 character wide. As you can see on the picture below this is ugly:
Example below looks nice:
I would like something like:
#Html.DisplayVerticalFor(x => x.MyText)
Any idea?
Thanks.
You can create an HTML helper DisplayVertical. (I am not adding the steps of how to create html helpers). The DisplayVertical will first split your text in character array and wrap each character inside a div or any other block level element, which can be inserted desired place.The implementation of DisplayVerticalFor can be something like this :
public static MvcHtmlString DisplayVertical (this HtmlHelper helper, string text)
{
string OutputString = "";
string assembleString = "<div>{0}</div>";
char[] textarr = text.ToCharArray();
foreach( char a in textarr )
{
OutputString += String.Format(assembleString, a);
}
return new MvcHtmlString(OutputString);
}
and in razor it will placed like this :
<div class="style-to-adjust-width-n-height"> #Html.DisplayVertical ("Sample") </div>
If you want to pass on a lambda expression to this html helper like this #Html.DisplayVerticalFor(x => x.MyText) then you need to add lambda expression parsing code to find out the text.
Lastly, this is a very rough code however you can add "TagBuilder" etc to make it more neat and clean.
I tried the following code in LINQPad and got the results given below:
List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s)
{
s.Trim();
});
listFromSplit.Dump();
"a" and " b"
so the letter b didn't get the white-space removed as I was expecting...?
Anyone have any ideas
[NOTE: the .Dump() method is an extension menthod in LINQPad that prints out the contents of any object in a nice intelligently formatted way]
you're just creating a trimmed string, not assigning anything to it.
var s = " asd ";
s.Trim();
won't update s, while..
var s = " asd ";
s = s.Trim();
will..
var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());
would, i suppose, be how i'd go about it.
The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.
You could do this:
s = s.Trim();
However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.
Filling a new list:
List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();
temp.ForEach(delegate(string s)
{
listFromSplit.Add(s.Trim());
});
listFromSplit.Dump();
Populating Manually:
string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();
foreach (string s in temp)
{
listFromSplit.Add(s.Trim());
};
listFromSplit.Dump();
Further to the answer posted by Adrian Kuhn you could do the following:
var result = listFromSplit.Select(s => s.Trim());
The string instances are immutable. Anything that seems to modify one, creates a new instance instead.
You are not assigning the trimmed result to anything. This is a classic error, I've only just got out of the habit of making this mistake with string.Replace :)
I have no IDE up and running, but this should get the job done (unless I am wrong):
var result = from each in listFromSplit select each.Trim();
Split on both spaces and commas and remove any empty entries. All nice and trimmed. Assumes that your strings don't contain spaces, though.
List<string> listFromSplit =
new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));
The linq options others have provided should work well. As another option, here is an extension method using a for loop:
public static void TrimCollection(this IList<string> stringCollection) {
for (int i = 0; i <= stringCollection.Count() - 1; i++)
stringCollection[i] = stringCollection[i].Trim();
}