How to format a very long text on a TextBox? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to format my text in TextBox?
My text value is:
00010001008002020100010000530997000014820000148200010000012C00001482000014820000148200010000012C000014820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000000000000000000000003F
And I want my output to be like this:
00010001-0080-02020100010000-53099700-00148200-00-14820001-0000-012C0000-14820000-1482000-0148-20001000-0012C000-0148-20000000-00000000-0000-00-000000000-00000000-0000-00000000-0000000-0000-00000000-00000000-00000000-0000-00000000-00-00000-000001010-00000000-000000000-0000-000000000-00000003-F

Your format must be fixed. It should not be dynamic.
I am just providing a logic you could append the further detail in regex string.
string mystring = "000100010080"
string regex = #"(\w{4})(\w{4})(\w{4})";
string strValue = Regex.Replace(mystring, regex, #"$1-$2-$3");
OUTPUT:
0001-0001-0080
EDITED: Take a look at complete example
string[] patern = "XXXXXXXX-XXXX-XXXXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XX-XXXXXXXX-
XXXX-XXXXXXXX-XXXXXXXX-XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXX-
XXXXXXXX-XXXXXXXX-XXXX-XX-XXXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-
XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XX-
XXXXX-XXXXXXXXX-XXXXXXXX-XXXXXXXXX-XXXX-XXXXXXXXX-XXXXXXXX-
X".Split('-');
string mystring = "00010001008002020100010000530997000014820000148200010000012C0
0001482000014820000148200010000012C00001482000000000000000000
0000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000001010000000000000000000000000000
00000000003F";
string regex = string.Empty;
string match = string.Empty;
for(int i=0; i<patern.Length;i++)
{
regex += #"(\w{" + patern[i].Length + "})";
match += "$" + (i + 1).ToString() + "-";
}
match = match.Substring(0, match.Length - 1);
txtMyTextBox.Text = Regex.Replace(mystring, regex, match);

Assuming that you have a fixed pattern of inserting hyphens and the string length is same every time, then you could do something like this:
int[] indices = new int[] { 2, 5, 11 };
string yourLongString = "blahblahblah";
foreach( var index in indices.Reverse() )
{
yourLongString = yourLongString.Insert( index - 1, "-" );
}
OR
Assuming you have no predefined pattern, you could insert hyphens anywhere and hence still you could use the same code above with the tweak to randomize the indices array, if needed.

Related

How can i replace characters in a string using c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have a string
"This is a test string for testing in dotnet4.8 (.net)" 53 chars
The requirement is that if the length of the string is greater than 40 characters
then to replace characters with "" starting from the (.net) moving to the left until the total chars is 40 , so i will end up with
"This is a test string for testing (.net)"
and it doesnt matter if the word is cut off , cause there could be scneario where the string is different but they will always end in (.net)
If I understood you right, you want the actual text to be 34 letters long, so when adding your "(.net)" ends up being 40.
so I guess something in the realms of:
public string Shorten(string s)
{
if(s.Length > 40)
return s.Substring(0,34) + "(.net)";
else
return s;
}
Try this:
const int maxLength = 40;
const string suffix = "(.net)";
var text = "This is a test string for testing in dotnet4.8 (.net)";
var exceed = text.Length - maxLength;
if (exceed > 0)
{
text = text.Substring(0, text.Length - exceed - suffix.Length) + suffix;
}
You could do this with Regex:
var input = "This is a test string for testing in dotnet4.8 (.net)";
var match = Regex.Match(input, #"^(.{0,34}).*?\(\.net\)$");
var result = $"{match.Groups[1].Value}(.net)";
That gives This is a test string for testing (.net).

String operations . Change the order and delete dome caracters of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a lot of string like this "01/08/2019" and i want to have a string like this "20190801" . I cant use the DateTime format , i must do it using string as a type . Please someone to help
You can use this
string date = "01/08/2019";
string result = string.Empty;
foreach(var item in date.Split('/'))
result = string.Concat(item, result);
what do you mean that you cannot use DateTime format?
Normally you should parse the format, keep it as DateTime in memory and use .ToString(format) for presentation purposes. Doing it all in one line would look like this:
DateTime.ParseExact("01/08/2019", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyyMMdd")
if you surely have '/' as separator,
you can split the string with '/', by this you will get string array.
you can loop on this array in descending order and keep concatenating the element of the array to form one single output.
string dateStr = "01/01/2019";
string[] dateElements = dateStr.Split('/');
string output = string.Empty;
for(int i = dateElements.Length - 1; i >= 0; i--)
{
output += dateElements[i];
}
You can write that code like that:
var input = #"01/08/2019";
var chrs = new[] {'/'};
var result = string.Concat(input.Where(c => !chrs.Contains(c)));
This way:
class Program
{
static void Main(string[] args)
{
string str = "01/08/2019";
string normalizedStr = Normalize(str);
}
private static string Normalize(string str)
{
return string.Join("-", str.Split(new char[] { '/' }).Reverse());
}
}
Basically it split the original string in many strings by char '/'
Then it reverse the order of these strings
Then it concatenate by using "-" as separator

How to extract string between same char [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have a string "ABD-DDD-RED-Large" and need to extract "DDD-RED"
using the Split I have:
var split = "ABD-DDD-RED-Large".Split('-');
var first = split[0];
var second = split[1];
var third = split[2];
var fourth = split[3];
string value = string.Join("-", second, third);
just wondering if there's a shorter code
If you just want the second and third parts of an always 4 part (delimited by -) string you can one line it with LINQ:
string value = string.Join("-", someInputString.Split('-').Skip(1).Take(2));
An input of: "ABD-DDD-RED-Large" would give you an output of: "DDD-RED"
Not enough information. You mentioned that string is not static. May be Regex?
string input = "ABD-DDD-RED-Large";
string pattern = #"(?i)^[a-z]+-([a-z]+-[a-z]+)";
string s = Regex.Match(input, pattern).Groups[1].Value;
Use regex
var match = Regex.Match(split, #".*?-(.*?-.*?)-.*?");
var value = match.Success ? match.Groups[1].Value : string.Empty;
I'm going out on a limb and assuming your string is always FOUR substrings divided by THREE hyphens. The main benefit of doing it this way is that it only requires the basic String library.
You can use:
int firstDelIndex = input.IndexOf('-');
int lastDelIndex = input.LastIndexOf('-');
int neededLength = lastDelIndex - firstDelIndex - 1;
result = input.Substring((firstDelIndex + 1), neededLength);
This is generic enough to not care what any of the actual inputs are except the hyphen character.
You may want to add a catch at the start of the method using this to ensure there are at least two hyphen's in the input string before trying to pull out the requested substring.

Move the selected string to first in comma separated string c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Move the selected string to first in comma separated string c#
If my csvstring is : C1,C2,C3
if my selectedstring is : C2
I require an output of : C2,C1,C3
Using the expression power of LINQ:
string cvsstring = "C1,C2,C3";
string selected = "C2";
cvsstring = string.Join(
",",
new[] { selected }.Concat(cvsstring.Split(',').Except(selected));
If there are more than one selected instances, the duplicated will be removed by this code. As the question is very unprecise about that I won't give a solution (using OrderBy, for example).
You can split, sort and join:
string csv = "C1,C2,C3";
string selected = "C2";
csv = String.Join(',', csv.Split(',').OrderBy(s => s == selected ? 0 : 1));
How about this?
var text ="C1,C2,C3";
var splits = text.Split(',');
var results =
String.Join(",",
Enumerable.Concat(
splits.Where(x => x == "C2"),
splits.Where(x => x != "C2")));
Maybe something like this:
var str= "C1,C2,C3";
var selected="C2";
var result= string.Join(",", str.Split(',').OrderBy (s =>s==selected?0:1));
It is pretty easy. Try below given code:
string csvstring = "C1,C2,C3";
string seletedstring = "C2";
string outputstring = selectedstring + csvstring.Replace(seletedstring .ToString(),"");
outputstring = outputstring.Replace(",,","");

Regular expressions : how to find [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why doesn't this regex, constructed like this:
tmprect = string "gg_rct_MyReg1"
regex = #"^\s*set\s+"+tmprect+#"\s*=\s*Rect\s*\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*,\s**(.+)\s*\).*$";
not work for
set gg_rct_MyReg1 = Rect (-704.0 , -352.0, 224.0 , 448.0) //rect 1
What did I do wrong?
///edited:
string findrectcoord = #"^\s*set\s+" + tmprect + #"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$";
StreamReader file3 = new StreamReader(openFileDialog1.FileName);
string line2;
while ((line2 = file3.ReadLine()) != null)
{
Regex foundrectr = new Regex(findrectcoord);
Match foundrectm = foundrectr.Match(line2);
if (foundrectm.Success)
{
MessageBox.Show("YES");
}
}
string:
set gg_rct_MyReg1 = Rect( -704.0 , -352.0, 224.0 , 288.0 ) //JassCode
Not Found
The regex itself, while ugly and inefficient, should work. You do need to assign the string you're adding into the regex before building the regex, though. While we're at it, let's clean it up:
string tmprect = "gg_rct_MyReg1";
Regex regexObj = new Regex(#"^\s*set\s+" + tmprect +
#"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$");
([^,\s]*) matches any number of characters except commas or spaces. That is more specific than .* which will match too much and force the regex engine to backtrack.

Categories