278 / 186 / 75
Регистрация: 12.04.2017
Сообщений: 1,088
|
17.11.2017, 16:39
|
3
|
Сообщение было отмечено Oksi999 как решение
Решение

Сообщение от Oksi999
ввести с клавиатуры предложение. Сделать вставку запятой перед словами, которые начинаются из букв «on».
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| string ChangeText(string text)
{
StringBuilder newText = new StringBuilder();
string prevWord = string.Empty;
string[] items = text.Split(new []{ ' ' },
System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < items.Length; i++)
{
if (!prevWord.EndsWith(",") && items[i].StartsWith("on"))
newText.Append(", ");
else
{
if (i > 0 && i < items.Length)
newText.Append(" ");
}
newText.Append(items[i]);
prevWord = items[i];
}
return newText.ToString();
} |
|
Добавлено через 12 минут
View
Кликните здесь для просмотра всего текста
MainWindow.xaml
XML | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid >
<Button Command="{Binding EditTextCommand}" Content="Insert Commas" HorizontalAlignment="Left" Height="36" Margin="365,232,0,0" VerticalAlignment="Top" Width="104"/>
<TextBox HorizontalAlignment="Left" Height="164" Margin="51,43,0,0" TextWrapping="Wrap"
Text="{Binding Path=CurrDocument.Content, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay }" VerticalAlignment="Top" Width="418"/>
</Grid>
</Window> |
|
MainWindow.xaml.cs
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
| namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new AppViewModel();
}
}
} |
|
Model
Кликните здесь для просмотра всего текста
Document.cs
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApp2
{
public class Document : INotifyPropertyChanged
{
private string content = String.Empty;
public string Content
{
get { return content; }
set
{
content = value;
OnPropertyChanged("Content");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
} |
|
ViewModel
Кликните здесь для просмотра всего текста
AppViewModel.cs
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| using System.Text;
namespace WpfApp2
{
public class AppViewModel
{
public AppViewModel()
{ }
private Document doc = new Document();
private RelayCommand editTextCommand;
public RelayCommand EditTextCommand
{
get
{
return editTextCommand ??
(editTextCommand = new RelayCommand(obj =>
{
doc.Content = ChangeText(doc.Content);
}));
}
}
public Document CurrDocument
{
get { return doc; }
set
{
doc = value;
}
}
string ChangeText(string text)
{
StringBuilder newText = new StringBuilder();
string prevWord = string.Empty;
string[] items = text.Split(new []{ ' ' },
System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < items.Length; i++)
{
if (!prevWord.EndsWith(",") && items[i].StartsWith("on"))
newText.Append(", ");
else
{
if (i > 0 && i < items.Length)
newText.Append(" ");
}
newText.Append(items[i]);
prevWord = items[i];
}
return newText.ToString();
}
}
} |
|
RelayCommand.cs
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| using System;
using System.Windows.Input;
namespace WpfApp2
{
public class RelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
}
} |
|
1
|