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 7 years ago.
Improve this question
I need to extract substring from string but starting with the first letter
example :
string s1 = "12 x 13 ABC 12# 15.8" substring = ABC 12# 15.8
string s2 = "25 x 32 FER #23.8" substring = FER #23.8
I tried the index of for the letter A or F but it didn't work
thanks
This should work (in case you use a non-letter character instead of x)
string SubstringThis(string input)
{
return new string(input.SkipWhile(c => !char.IsLetter(c)).ToArray());
}
Simple utility function:
string SubstringFromFirstLetter(string s)
{
for (int i=0; i < s.Length; ++i)
{
if (char.IsLetter(s[i]))
{
return s.Substring(i);
}
}
return "";
}
Keep in mind that x is a letter. Do you want it to match only capitalized letters?
public static string GetSubstringStartingWithFirstAlphaCharacter(this string toEvaluate)
{
const string pattern = "([a-zA-Z])(.+)";
var regex = new Regex(pattern);
return regex.Match(toEvaluate).Value;
}
Related
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).
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
I have a function with takes something like a SSN number as argument. The input can be in one of the following formats (validity of SSN is not a factor)
999-99-9999
99-999-9999
9-9999-9999
999999999
99999999-9
Hyphen can be at any location and input can be of any length.
This method will create a random number with same length of input and the output will have hyphens on same locations as input
for example, if input 9-9999-9999 is passed then random output could be 1-2234-5678 (match hyphen location)
for example, if input 99999999-9 is passed then random output could be 12234567-8 (match hyphen location)
public String GenerateNumber(String input)
{
//find the location of hyphens in the input
String output = Generate random number of same length as input //ToString();
//put hyphens on the random number generated above at the same locations matching input
return output
}
Would be helpful if sample code provided in C# but java would also work.
Here's one way of doing it.
public string GenerateNumber(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
throw new ArgumentNullException("input");
}
Random rand = new Random();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '-')
{
builder.Append("-");
}
else
{
builder.Append(rand.Next(0, 9).ToString());
}
}
return builder.ToString();
}
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.
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
I have some problems with split and check string.
I need to split string, replace halfs and check is this the same as the second string.
example: first string = tokyo second string = koyto
soo... S = a+b = b+a
S - a = b and S - b = a
a and b is part of one string (S) and may have different long in this case a = to and b = koy
first I need to check string length - is the are different - then write Error - it's easy
the I thought that I can compare strings in ASCII (case sensitivity is not important) and it' could be ok but...
I can create string tooky which have got the same size in ASCII but is not created from split and invert parts of first string...
any ideas?
static void Main(string[] args)
{
string S = "tokyo";
string T = "kyoto";
if (S.Length == T.Length)
{
split string ?
}
else
Console.WriteLine("This two words are different. No result found.");
Console.Read();
}
I would suggest doing the comparisons with strings. You can use the String.ToLower() method to convert them both to lowercase for comparison.
I am not exactly sure what problem you are trying to solve is, but from what I understand you are trying to check if string S can be split into two substrings that can be rearranged to make string T.
To check this you will want something similar to the following
for (int i = 0; i < S.length; i++) {
string back = S.substring(i);
string front = S.substring(0,i);
if (T.equals(back + front))
result = true;
}
Hope this helps
If you want to compare equality of two collections you should consider using LINQ:
static void Main(string[] args)
{
string S = "tokyo";
string T = "kyoto";
if (S.Length == T.Length)
{
if (S.Intersect(T).Any())
{
Console.WriteLine("The Contents are the same");
Console.Read();
}
}
else
Console.WriteLine("This two words are diferent. No result found.");
Console.Read();
}
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
Example:
If there is a line http://google.com/adi/727412;sz=728x90;ord=$RANDOM? which contains adi in it, wants it to be replaced with http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM? and rest all other text to be same with no change.
Please help
This is a fairly simple task:
string url = #"http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if (url.Contains(#"/adi/"))
{
int pos = url.IndexOf(";ord"); //// Find first occurence of Ord parameter
url = url.Insert(pos, ";click=$CLICK"); //// Insert text at position
}
Edit: To accomplish the task for multiple occurences I used a solution from this thread.
{
string url = "<google.com/adi/727412;sz=728x90;ord=$RANDOM?>; <google.com/adi/727412;sz=300x250;ord=$RANDOM?>";
string searchString = #"/adi/";
int n = 0;
while ((n = url.IndexOf(searchString, n)) != -1)
{
n += searchString.Length;
int pos = url.IndexOf('?', n);
url = url.Insert(pos, ";click=$CLICK");
}
}
string url = "http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if(url.Contains("adi")) url = "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?";
string url = "blablablablablahttp://google.com/adi/727412;sz=728x90;ord=$RANDOM?blablabla";
if(url.Contains("adi")) url.Replace("http://google.com/adi/727412;sz=728x90;ord=$RANDOM?", "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?");