Am newbie here and tried the search, but not quite understood it, so I am thinking to ask to the forum for help.
I want to get the result into the text box from the following code but got an error.
Confused on how to overcome it, appreciate for any help. I believe it was an error on the conversion from linqIgroup to string to be put in textboxt.Text
It's about to display the most word(s) that has been occurred in a text file.
string sentence;
string[] result = {""};
sentence = txtParagraph.Text;
char[] delimiters = new char[] { ' ', '.', '?', '!' };
string[] splitStr = sentence.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
var dic = splitStr.ToLookup(w => w.ToLowerInvariant());
var orderedDic = dic.OrderByDescending(g => g.Count(m=>m.First()).ToString()));
txtFreqWord.Text = orderedDic.ToString();
Try the following to do what you are after. I am using regular expressions aswell.
var resultsList = System.Text.RegularExpressions.Regex.Split("normal text here normal normal".ToLower(), #"\W+")
.Where(s => s.Length > 3)
.GroupBy(s => s)
.OrderByDescending(g => g.Count());
string mostFrequent = resultsList.FirstOrDefault().Key;
To get all of them with their count, do the following :
foreach (var x in resultsList) {
txtFreqWord.Text = txtFreqWord.Text + x.Key + " " + x.Count() +", ";
}
Related
If String a = "abbc" and String b="abc", we have to print that character 'b' is missing in the second string.
I want to do it by using Java. I am able to do it when String 2 has a character not present in String 1 when s1=abc and s2=abk but not when characters are same in both strings like the one I have mentioned in the question.
public class Program
{
public static void main(String[] args) {
String str1 = "abbc";
String str2 = "abc";
char first[] = str1.toCharArray();
char second[] = str2.toCharArray();
HashMap <Character, Integer> map1 = new HashMap<Character,Integer>();
for(char a: first){
if(!map1.containsKey(a)){
map1.put(a,1);
}else{
map1.put(a,map1.get(a)+1);
}
}
System.out.println(map1);
HashMap <Character, Integer> map2 = new HashMap<Character,Integer>();
for(char b: second){
if(!map2.containsKey(b)){
map2.put(b,1);
}else{
map2.put(b,map2.get(b)+1);
}
}
System.out.println(map2);
}
}
I have two hashmaps here one for the longer string and one for the shorter string, map1 {a=1,b=2,c=1} and map2 {a=1,b=1,c=1}. What should I do after this?
Let assume that we have two strings a and b.
(optional) Compare lengths to find longer one.
Iterate over them char by char and compare letters at same index.
If both letters are the same, ignore it. If different, add letter from longer string to result and increment index of the longer string by 1.
What's left in longer string is your result.
Pseudocode:
const a = "aabbccc"
const b = "aabcc"
let res = ""
for (let i = 0, j = 0; i <= a.length; i++, j++) {
if (a[i] !== b[j]) {
res += a[i]
i++
}
}
console.log(res)
More modern and elegant way using high order functions:
const a = "aabbccc"
const b = "aabcc"
const res = [...a].reduce((r, e, i) => e === b[i - r.length] ? r : r + e, "")
console.log(res)
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
This email string convert to this patter like
Fox Example
email -> ab****j#email.com
phoneNumber -> (012)3****89
Please help using RegExp and 0ther technics are most welcome.
Try this way
void main() {
var result = 'nilesh.rathod#gmail.com'.replaceAll(new RegExp('(?<=.)[^#](?=[^#]*?[^#]#)'), '*');
print(result);
}
You can use the replaceRange method
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.replaceRange(2, email.indexOf("#")-1, hiderPlaceholder);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3).replaceRange(1, phoneNumber.substring(3).length-2, hiderPlaceholder);
print (censuredEmail);
print (censuredPhoneNumber);
Or you can just go for the evergreen substring
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.substring(0, 2) + hiderPlaceholder + email.substring(email.indexOf("#")-1);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 4) + hiderPlaceholder + phoneNumber.substring(phoneNumber.length-2);
print (censuredEmail);
print (censuredPhoneNumber);
P.s. obviously, add all the controls you want, e.g. for the length of the email/phone number
Does this work? I'm trying to print a message in this.
char[] tempMessage = message.toCharArray();
String[] message2 = message.split(" ");
Integer.toString(number).toCharArray();
for(int x = 0; x<newMessage.length; x++)
{
}
Although its better to use a StringBuilder, I can show it using String(s).
String[] strArr = "hello world".split("\\s+");
String s = String.valueOf(strArr[0].charAt(0))+strArr[0].length()+String.valueOf(strArr[1].charAt(0))+strArr[1].length();
Output : h5w5
String[] message2 = message.split("\\s+");
String output = "";
for(int i = 0; i < message2.length; i++)
{
output += "" + message2[i].charAt(0) + message2[i].length();
}
//output has output string.
TheLostMind's solution is already good, but I think it needs a solution for Strings of arbitrary length.
String outputString = "";
for(String x : message.split("\\s+"))
{
outputString = outputString.concat(x.charAt(0) + x.length());
}
As stated in the comments, this solution is very similiar to brso05's solution. The difference is in using the :-Operator in the for-loop. It's shorter and IMHO easier to understand, as it says 'for each String in the resulting array'.
Also, using the concat()-function is considered safer in my work environment.
I made a console program, but the problem is that it doesn't allow parameters to be inserted. So I'm wondering how would I split a single string into multiple strings to achieve what I need. E.g.: text="msg Hello" would be split into textA="msg" and textB="Hello"
This is the main console code so far (just to show the idea):
if (keyboard_check_pressed(vk_enter)) {
text_console_c = asset_get_index("scr_local_"+string(keyboard_string));
if (text_console_c > -1) {
text_console+= "> "+keyboard_string+"#";
script_execute(text_console_c);
text_console_c = -1;
}
else if (keyboard_string = "") {
text_console+= ">#";
}
else {
text_console+= "> Unknown command: "+keyboard_string+"#";
};
keyboard_string = "";
}
I cant recommend spliting string with iteration by char, because when u try split very very very long string, then time to split is very long and can freeze thread for a short/long time. Game maker is single threaded for now.
This code is much faster.
string_split
var str = argument[0] //string to split
var delimiter = argument[1] // delimiter
var letDelimiter = false // append delimiter to each part
if(argument_count == 3)
letDelimiter = argument[2]
var list = ds_list_create()
var d_at = string_pos(delimiter, str)
while(d_at > 0) {
var part = string_delete(str, d_at , string_length(str))
if(letDelimiter)
part = part + delimiter
str = string_delete(str, 1, d_at)
d_at = string_pos(delimiter, str)
ds_list_add(list, part)
if(d_at == 0 && str != "")//last string without delimiter, need to add too
ds_list_add(list, str)
}
return list;
Dont forget ds_list_destroy after you iterate all strings
for example:
var splited = string_split("first part|second part", '|')
for(splited) {
//do something with each string
}
ds_list_destroy(splited)
Something like this may help, haven't tested it out but if you can follow what is going on its a good place to start.
Text = "msg Hello"
counter = 0
stringIndex = 0
for (i = 0; i < string_length(text); i++)
{
if string_char_at(text,i) == " "
{
counter++
stringIndex = 0
} else {
string_insert(string_char_at(text,i),allStrings(counter),stringIndex)
stringIndex++
}
}
allStrings should be an array containing each of the separate strings. Whenever a " " is seen the next index of allStrings starts having it's characters filled in. stringIndex is used to add the progressive characters.
I want to display a string in the format ip1,ip2,ip3,ip4 etc in 'N' number of times. Can any one help me to solve this c# programme.
I assume this is what you want:
int n = 10;
IEnumerable<String> ips = Enumerable.Range(1, n).Select(i => "ip" + i);
String allIPs = String.Join(",", ips);
Result:
ip1,ip2,ip3,ip4,ip5,ip6,ip7,ip8,ip9,ip10
Demo
var myIps = String.Join(", ",(Enumerable.Range(1, 10).Select(i => "ip" + i)));
myIps will be ip1, ip2, ip3....ip10