Extract phone number from text [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to build a method that will get a string (preferably the text of a textblock) and it will identify and highlight any phone numbers in the string. The goal is to enable the user to tap any number and directly call or text it(by using the appropriate launcher).
How can I work this out? Any ideas? Thank you in advance!

You can use Regular expression to do this.
Example:-
var s= new Regex(#"(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}",
RegexOptions.IgnoreCase); //North American number
var text = "Some Texxt";
MatchCollection m= s.Matches(text);

String s = "abc055667788abc";
string phoneNumber;
foreach(char c in s)
{
if(Char.isNumber(c) || c == " " || c == "+")
{
phoneNumber = phoneNumber + c;
minimumDigits++;
if(minimumDigits >= 9)
{
NumberDetected(phoneNumber);
}
}
else
{
minimumDigits = 0;
}
}
NumberDetected(string rawNumber)
{
int plusses = 0;
foreach(char c in rawNumber)
{
if(c == "+")
{
plusses++;
}
}
if(plusses <= 1)
{
if(rawNumber.StartsWith("+")
{
NumberDone(rawNumber);
}
}
else
{
MessageBox.Show("Number contained too many plusses!");
}
}

Related

Extract substring from the first letter [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 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;
}

Extract the first two words from a string with C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I guys im trying to workout C# code to extract the first two words from string. below is code im doing.
public static string GetDetailsAsString(string Details)
{
string Items = //how to get first 2 word from string???
if (Items == null || Items.Length == 0)
return string.Empty;
else
return Items;
}
Define "words", if you want to get the first two words that are separated by white-spaces you can use String.Split and Enumerable.Take:
string[] words = Details.Split();
var twoWords = words.Take(2);
If you want them as separate string:
string firstWords = twoWords.First();
string secondWord = twoWords.Last();
If you want the first two words as single string you can use String.Join:
string twoWordsTogether = string.Join(" ", twoWords);
Note that this simple approach will replace new-line/tab characters with empty spaces.
Assuming the words are separated by whitespaces:
var WordsArray=Details.Split();
string Items = WordsArray[0] + ' ' + WordsArray[1];

C# - split string [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
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();
}

C# read only part of whole string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a string which has a following format:
"####/xxxxx"
The text before the "/" is always an integer and I need to read it. How do I get only the integer part of this string (before the "/")?
Thank you for your help.
You can split the string on / and then use int.TryParse on the first element of array to see if it is an integer like:
string str = "1234/xxxxx";
string[] array = str.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
int number = 0;
if (str.Length == 2 && int.TryParse(array[0], out number))
{
//parsing successful.
}
else
{
//invalid number / string
}
Console.WriteLine(number);
Use IndexOf and Substring:
int indexOfSlash = text.IndexOf('/');
string beforeSlash = null;
int numBeforeSlash = int.MinValue;
if(indexOfSlash >= 0)
{
beforeSlash = text.Substring(0, indexOfSlash);
if(int.TryParse(beforeSlash, out numBeforeSlash))
{
// numBeforeSlash contains the real number
}
}
Another alternative: use a regular expression:
var re = new System.Text.RegularExpression(#"^(\d+)/", RegexOptions.Compiled);
// ideally make re a static member so it only has to be compiled once
var m = re.Match(text);
if (m.IsMatch) {
var beforeSlash = Integer.Parse(re.Groups[0].Value);
}

Custom function on Regex replace in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to replace all words in a text by applying a specific replacement method Modify(). I have the following code snippet in C#:
Regex regex = new Regex("[A-Za-z][a-z]*");
regex.Replace(text, x => Modify(x.Value));
The Modify() function is some function that is executed to modify each match, for example it could replace all the characters in a word with the next alphabetical character. For example, if this is the input text:
Magic banana is eating the apple.
This could be the output:
Nbhjd cbobob jt fbujoh uif bqqmf.
The purpose of the Modify() function is irrelevant here. I am wondering about the Java implementation of the MatchEvaluator. The code is fairly simple in C#, but how would this be achieved in Java?
How about something along this lines:
public static void main(String[] args) {
String text = "Magic banana is eating the apple.";
System.out.println("Old text: " + text);
System.out.println("New text: " + getEditedText(text));
}
private static String getEditedText(String text) {
StringBuffer result = new StringBuffer();
Pattern pattern = Pattern.compile("[A-Za-z][a-z]*");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
matcher.appendReplacement(result, getReplacement(matcher));
}
matcher.appendTail(result);
return result.toString();
}
private static String getReplacement(Matcher matcher) {
String word = matcher.group(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
sb.append((char)(c + 1));
}
return sb.toString();
}
This is a slightly edited example of the code that can be found at the bottom of this page.
This is the output you would get:
Old text: Magic banana is eating the apple.
New text: Nbhjd cbobob jt fbujoh uif bqqmf.

Categories